遍历key,value,key-value
package com.atguigu.exer;
import java.util.*;
/**
* @author JessieWu
* @create 2020-08-12 21:47
*/
public class Test {
public static void main(String[] args) {
//遍历key
HashMap<String,Integer> map = new HashMap<>();
map.put("wzq",12);
map.put("wmn",123);
map.put("qwe",125);
map.put("wzadasq",712);
map.put("wzadsdsaq",3312);
Set<String> strings = map.keySet();
Iterator<String> iterator = strings.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//遍历values
Collection<Integer> coll = map.values();
for(Integer c:coll){
System.out.println(c);
}
//遍历key-value
Set<Map.Entry<String,Integer>> entrySet = map.entrySet();
Iterator<Map.Entry<String,Integer>> iterator1 = entrySet.iterator();
while(iterator1.hasNext()){
Map.Entry<String, Integer> next = iterator1.next();
String key = next.getKey();
Integer value = next.getValue();
System.out.println(key + "--->" + value);
}
}
}