码迷,mamicode.com
首页 > 编程语言 > 详细

java 迭代器

时间:2020-03-28 19:46:44      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:import   迭代   big   cannot   ext   system   lang   java   main   

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);
        }



    }


}

  

java 迭代器

标签:import   迭代   big   cannot   ext   system   lang   java   main   

原文地址:https://www.cnblogs.com/SunshineKimi/p/12588773.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!