HashMap遍历之EntrySet————小练习

   public static void main(String[] args) {
        HashMap hashMap = new HashMap();
        hashMap.put("jack",650);
        hashMap.put("tom",1200);
        hashMap.put("smith",2900);

        System.out.println(hashMap);
        //将jack的工资更改为2600
        hashMap.put("jack",2600);
        System.out.println(hashMap);
        Set entrySet = hashMap.entrySet();

        //将每个价钱自增100员
        System.out.println("=========自增100元之后的薪水========");
        Set setkey = hashMap.keySet();
        for (Object key :setkey) {
            hashMap.put(key,(Integer)hashMap.get(key)+100);
            /**将hashmap.get()的类型为:java.lang.Class
             * 需要向下转型为Integer才能与int类型的数字相加
             */
        }

        System.out.println(hashMap);

        //遍历集合的key值
        System.out.println("=====遍历集合的key值====");
        Iterator iterator1 = hashMap.keySet().iterator();
        while (iterator1.hasNext()) {
            Object next =  iterator1.next();
            System.out.println(next);
        }


        //使用values遍历集合的values值
        System.out.println("=====遍历集合的values值=====");
        Iterator iterator = hashMap.values().iterator();
        while (iterator.hasNext()) {
            Object next =  iterator.next();
            System.out.println(next);
        }

        //使用Entry遍历集合的values值
        System.out.println("======使用Entry遍历集合的values值=====");
        Set entryset = hashMap.entrySet();
        Iterator iterator2 = entryset.iterator();
        while(iterator2.hasNext()){
            Object next = iterator2.next();
            Map.Entry entry = (Map.Entry)next;
            System.out.println(entry.getValue()+"+"+entry.getKey());
        }

//        Set entryset = entrySet;
//        Iterator iterator2 = entryset.iterator();
//        while (iterator2.hasNext()){
            Object next = iterator2.next();
//            Map.Entry next = (Map.Entry)iterator2.next();
//            System.out.println(next.getValue()+" "+next.getKey());
//        }
    }
}

HashMap遍历之EntrySet————小练习_第1张图片 

你可能感兴趣的:(韩顺平java习题,java,python,开发语言)