Iterator 的使用

public static void main(String[] args) {
    List l = new ArrayList<>();
    l.add("张三 ");
    l.add("李四");
    l.add("王五");
    /*
    Iterator iter = l.iterator();
    while (iter.hasNext())
    {
        String str = (String) iter.next();
        System.out.println(str);
    }*/

    for(Iterator iter = l.iterator();iter.hasNext();){
        String str =(String) iter.next();
        System.out.println(str);
    }


    Map <String,String > map = new  HashMap<>();
    map.put("1", "张三");
    map.put("2", "李四");
    map.put("3", "王五");

    Iterator iter = map.entrySet().iterator();
    while (iter.hasNext()){
        Map.Entry<String, String> entry = (Entry<String, String>) iter.next();
        String key = entry.getKey();
        String value = entry.getValue();
        System.out.println(key + value);
    }
    /*
    Set<String> ls = map.keySet();
    for(String str :ls){
        System.out.println("key=" + str + "value=" +map.get(str));
    }*/

}

你可能感兴趣的:(Iterator 的使用)