zoukankan      html  css  js  c++  java
  • 使用Iterator迭代器遍历容器元素(List/Set/Map)

      迭代器为我们提供了统一的遍历容器的方式,参见以下示例代码:

    【示例】迭代器遍历List

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    public class Test {
        public static void main(String[] args) {
            List<String> aList = new ArrayList<String>();
            for (int i = 0; i < 5; i++) {
                aList.add("a" + i);
            }
            System.out.println(aList);
            for (Iterator<String> iter = aList.iterator(); iter.hasNext();) {
                String temp = iter.next();
                System.out.print(temp + " ");
                if (temp.endsWith("3")) {// 删除3结尾的字符串
                    iter.remove();
                }
            }
            System.out.println();
            System.out.println(aList);
        }
    }

         执行结果如图所示:

    图9-27示例9-11运行效果图.png

    老鸟建议

          如果遇到遍历容器时,判断删除元素的情况,使用迭代器遍历!

    【示例】迭代器遍历Set

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public class Test {
        public static void main(String[] args) {
            Set<String> set = new HashSet<String>();
            for (int i = 0; i < 5; i++) {
                set.add("a" + i);
            }
            System.out.println(set);
            for (Iterator<String> iter = set.iterator(); iter.hasNext();) {
                String temp = iter.next();
                System.out.print(temp + " ");
            }
            System.out.println();
            System.out.println(set);
        }
    }

          执行结果如图所示:

    图9-28示例9-12运行效果图.png

    【示例】迭代器遍历Map一

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public class Test {
        public static void main(String[] args) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("A""高淇");
            map.put("B""高小七");
            Set<Entry<String, String>> ss = map.entrySet();
            for (Iterator<Entry<String, String>> iterator = ss.iterator(); iterator.hasNext();) {
                Entry<String, String> e = iterator.next();
                System.out.println(e.getKey() + "--" + e.getValue());
            }
        }
    }

          执行结果如图所示:

    图9-29示例9-13运行效果图.png

          我们也可以通过map的keySet()、valueSet()获得key和value的集合,从而遍历它们。

    【示例】迭代器遍历Map二

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public class Test {
        public static void main(String[] args) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("A""高淇");
            map.put("B""高小七");
            Set<String> ss = map.keySet();
            for (Iterator<String> iterator = ss.iterator(); iterator.hasNext();) {
                String key = iterator.next();
                System.out.println(key + "--" + map.get(key));
            }
        }
    }

          执行结果如图所示:

    图9-30示例9-14运行效果图.png

  • 相关阅读:
    项目打包文件build.xml
    【转】常见面试之机器学习算法思想简单梳理
    【转】11位机器学习大牛最爱算法全解
    Simplify Path
    Text Justification
    Valid Number
    Substring with Concatenation of All Words
    Shortest Palindrome
    Palindrome Pairs
    Decode Ways
  • 原文地址:https://www.cnblogs.com/huaxiansheng/p/15317787.html
Copyright © 2011-2022 走看看