Map<Integer, String> maplist = new HashMap<>();
maplist.put(1, "xiaoli");
maplist.put(2, "xiao");
maplist.put(3, "xiaoming");
maplist.put(4, "li");
maplist.put(5, "ming");
maplist.put(5, "xiao");
// 使用Lambda表达式 遍历
maplist.forEach((key, value)->{
System.out.println("key: " + key + ", value: " + value);
});
// 使用Stream API遍历entry
maplist.entrySet().stream().forEach(map->{
System.out.println(map.getKey()+": "+map.getValue());
});
// for循环
for (Map.Entry<Integer, String> entry : maplist.entrySet()) {
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " -> " + value);
}
for (Integer key : maplist.keySet()) {
String value = maplist.get(key);
System.out.println(key+"->>>>"+value);
}
// 迭代器
Iterator<Entry<Integer, String>> it = maplist.entrySet().iterator();
while(it.hasNext()){
Entry<Integer, String> entry = it.next();
System.out.println("key:"+entry.getKey()+" key:"+entry.getValue());
}