private static Map<String, Object> map = new HashMap<>();
private static Map<String, Integer> unsorted = new HashMap<>();
static {
map.put("Paris", "巴黎");
map.put("London", "伦敦");
map.put("NewYork", "纽约");
unsorted.put("a", 2);
unsorted.put("b", 1);
unsorted.put("c", 3);
}
public static void main(String[] args) {
Object hk1 = map.get("HongKong");
Object hk2 = map.getOrDefault("HongKong", "香港");
map.forEach((k, v) -> System.out.println(k + " = " + v));
Object result1 = map.putIfAbsent("HongKong", "香港");
Object result2 = map.putIfAbsent("HongKong", "香干");
boolean bool1 = map.remove("HongKong", "香干");
boolean bool2 = map.remove("HongKong", "香港");
Object value1 = map.replace("HongKong", "香港");
Object value2 = map.replace("NewYork", "纽约!纽约");
boolean bool3 = map.replace("Paris", "巴黎", "巴黎!巴黎");
map.replaceAll((k, v) -> k + ", " + v);
map.forEach((k, v) -> System.out.println(k + " = " + v));
Object obj1 = map.compute("HongKong", (k, v) -> k);
Object obj2 = map.compute("HongKong", (k, v) -> null);
unsorted.forEach((k, v) -> System.out.println(k + " = " + v));
Map<String, Integer> sorted = new LinkedHashMap<>();
unsorted.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEach((e) -> sorted.put(e.getKey(), e.getValue()));
sorted.forEach((k, v) -> System.out.println(k + " = " + v));
sorted.clear();
unsorted.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue().reversed()).forEach((e) -> sorted.put(e.getKey(), e.getValue()));
sorted.forEach((k, v) -> System.out.println(k + " = " + v));
}