本文主要是介绍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的工资更改为2600hashMap.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<capture#1, 共 ? extends java.lang.Object>* 需要向下转型为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————小练习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!