迭代器遍历Map、List、Set

1、Map的遍历
    Map<Integer,String> maps=new HashMap<Integer,String>();
    maps.put(1,"a");
    maps.put(2,"b");
    maps.put(3,"c");
    
    Iterator<Map.Entry<Integer,String>> itMap=map.entrySet.iterator();
    while(itMap.hasNext){
        Map.Entry<Integer,String> entry=itMap.next();
        System.out.println(entry.getKey()+","+entry.getValue);    
    }    

2、List的遍历
    List<String> lists=new ArrayList<String>();
    lists.add("a");
    lists.add("b");
    lists.add("c");
    
    Iterator<String> itList=lists.iterator();
    while(itList.hasNext()){
        String value=itList.next();
        System.out.println(value);
    }    

3、Set的遍历
    Set<String> set=HashSet<String>();
    set.add("a");
    set.add("b");
    set.add("c");
    
    Interator itSet=set.interator();
    for(itSet.hasNext()){
        String value=itSet.next();
        System.out.println(value);
    }
        

  

猜你喜欢

转载自www.cnblogs.com/kongnengjing/p/9434339.html