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排序

上一篇:oracle修改用户密码的方法


下一篇:emlog博客程序几行代码统计网站在线人数