java
复制代码
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<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 = 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());
}
}
}