1. 程式人生 > >Map 遍歷方法

Map 遍歷方法

Hashtable<String, Integer> hashtable = new Hashtable<String, Integer>(){
            {
                put("0", 1);
                put("xixi", 2);
                put("haha", 3);
            }
        };
        for(String set: hashtable.keySet()){
            System.out.println("key:"+set+",value:"+hashtable.get(set));
        }
        System.out.println("Iterator-----");
        Iterator<Map.Entry<String, Integer>> it = hashtable.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry<String, Integer> entry = it.next();
            System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());
        }

        System.out.println("-----------------------------------------------");
        HashMap<String, Integer> hashMap = new HashMap<String, Integer>(){
            {
                put("1", 1);
                put("2", 1);
                put("3", 1);
                put("4", 1);
                put("5", 5);
            }
        };
        for(Map.Entry<String, Integer> entry : hashMap.entrySet()){
            System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());
        }