在this oracle java 它说:
Along similar lines, suppose you want to know whether two Map objects contain mappings for all of the same keys.
if (m1.keySet().equals(m2.keySet())) { ... }
我完全理解它的工作原理以及它的工作原理。然而,做这样的事情并不容易:
if (m1.equals(m2)){
..}
或者出于其他原因,我认为使用 Collection View 更好?
提前致谢。
最佳答案
映射可以有相同的键但不同的值:
Map<String, String> m1 = new HashMap<>();
m1.put("x", "1");
m1.put("y", "2");
Map<String, String> m2 = new HashMap<>();
m2.put("x", "1");
m2.put("y", "4");
System.out.println(m1.equals(m2)); // false
System.out.println(m1.keySet().equals(m2.keySet())); // true
关于java - 按键集比较 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15684299/