how to compare two hash maps?

后端 未结 7 1037
独厮守ぢ
独厮守ぢ 2021-02-08 02:22

How to compare the values in both hash maps with the help of keys ? Since the keys are identical whereas values are\'nt. and return boolean result for each key comparision. like

7条回答
  •  盖世英雄少女心
    2021-02-08 02:52

    Assuming that you have the same entry sets:

    import java.util.HashMap;
    import java.util.Map.Entry;
    
    public class HashMapComparison {
    
        public static void main(String[] args) {
    
            HashMap map1 = new HashMap();
            map1.put(1, "res");
            map1.put(2, "tr");
    
            HashMap map2 = new HashMap();
            map2.put(1, "res");
            map2.put(2, "cd");
    
            for (Entry entry : map1.entrySet()) {
                Integer key = entry.getKey();
                System.out.println("key " + key + ": "
                        + entry.getValue().equals(map2.get(key)));
            }
    
        }
    
    }
    

提交回复
热议问题