Comparing two hashmaps for equal values and same key sets?

后端 未结 6 961
北荒
北荒 2020-12-03 02:18

How can I best compare two HashMaps, if I want to find out if none of them contains different keys than the other, and if the values of that keys match each oth

6条回答
  •  独厮守ぢ
    2020-12-03 03:19

    /* JAVA 8 using streams*/
       public static void main(String args[])
        {
            Map map = new HashMap();
            map.put(100, true);
            map.put(1011, false);
            map.put(1022, false);
    
            Map map1 = new HashMap();
            map1.put(100, false);
            map1.put(101, false);
            map1.put(102, false);
    
            boolean b = map.entrySet().stream().filter(value -> map1.entrySet().stream().anyMatch(value1 -> (value1.getKey() == value.getKey() && value1.getValue() == value.getValue()))).findAny().isPresent();
            System.out.println(b);
        }
    

提交回复
热议问题