Map操作
作者:互联网
Map遍历
- for-each循环中使用entries来遍历
Map<Integer, String> map = new HashMap<>();
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
- 在for-each循环中遍历keys或values
Map<Integer, String> map = new HashMap<>();
//遍历map中的键
for (Integer key : map.keySet()) {
System.out.println("Key = " + key);
}
//遍历map中的值
for (String value : map.values()) {
System.out.println("Value = " + value);
}
- 使用Iterator遍历
Map<Integer, String> map = new HashMap<>();
Iterator<Map.Entry<Integer, String>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Integer, String> entry = entries.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
Map排序
标签:Map,遍历,map,System,entries,entry,操作 来源: https://blog.csdn.net/ycjiang00/article/details/120445253