java使用map去重复

public class Test {
    public static void main(String[] args) {
        Map<Number, String> map1 = new HashMap<Number, String>();
        Map<Number, String> map2 = new HashMap<Number, String>();
        
        map1.put(1, "a");
        map1.put(2, "b");
        map1.put(3, "c");
        map1.put(4, "d");
        map1.put(5, "e");
        
        map2.put(1, "g");
        map2.put(2, "f");
        map2.put(3, "c");
        map2.put(4, "t");
        map2.put(5, "e");
        
        boolean isEquals = false;
        
        for(Object o : map1.keySet()){
            isEquals = map2.containsKey(o);
            if(isEquals){
                isEquals = map1.get(o).equals(map2.get(o));
            }
            if(isEquals){
                map2.remove(o);
            }
        }
        for(Object o : map2.keySet()){
            System.out.println(o.toString() + ":" + map2.get(o));
        }
    }
}

 

posted @ 2020-09-02 17:34  红尘沙漏  阅读(2524)  评论(0编辑  收藏  举报