import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MapDemo {
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>();
map.put(1,"Lily");
map.put(2,"Jack");
map.put(3,"Jerry");
//Map.Entry循环
for (Map.Entry<Integer,String> entry:map.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
//迭代器遍历
Iterator<Map.Entry<Integer,String>> it = map.entrySet().iterator();
while (it.hasNext()){
Map.Entry<Integer,String> entry = it.next();
System.out.println(entry.getKey()+"->"+entry.getValue());
}
}
}
结果
1:Lily
2:Jack
3:Jerry
1->Lily
2->Jack
3->Jerry
Process finished with exit code 0