java 迭代器

吃可爱长大的小学妹 提交于 2020-03-28 19:05:04
package iter;
import java.util.*;

public class iterdemo {
    public static void main(String[] args) {

        // 用来迭代容器的,next() ,hasNext(),set 不可以for循环
        Set set = new HashSet();
        Set set2 =new TreeSet();
        set.add(1);
        set.add(2);
        Iterator it =set.iterator();
        while(it.hasNext()){
//            Integer integer = (Integer)it.next();
            int integerss = (int)it.next();
            System.out.println(integerss);
        }

        HashMap<String,Object> map =new HashMap<>();
        map.put("k1","2333");
        map.put("k2",555);
        //map iter 1
        Set keySet = map.keySet();
        Iterator it2 = keySet.iterator();
        while (it2.hasNext()){
            String key =(String) it2.next();
            String v = map.get(key).toString();  // java.lang.Integer cannot be cast to java.lang.String强转
            System.out.println(key+" : "+v);

        }
        //map 2 iter,todo by keySet iter
        Set entryset = map.entrySet(); // key|value   k2|v2
        Iterator it3 = entryset.iterator();
        while (it3.hasNext()){
            Map.Entry entry = (Map.Entry) it3.next();
            String key =entry.getKey().toString();
            String value =entry.getValue().toString();
            System.out.println(key+": "+value);
        }

        //map3 bigMap,,for (Map.Entry<String,String> entry :map.entrySet())
        for (Map.Entry<String,Object> entry :map.entrySet()) {
            String key=entry.getKey();
            String value=entry.getValue().toString();
            System.out.println(value+":"+key);
        }



    }


}

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!