日期:2014-05-19  浏览次数:20607 次

集合带泛型删除另个集合里相同(重复实体)
Java code


private static List removeAll(List big,List small)throws Exception{
        
        LinkedList link = new LinkedList(big);
        HashSet set = new HashSet(small);
        Iterator iter = link.iterator();
        while(iter.hasNext()){
            if(set.contains(iter.next())){
                iter.remove();
            }
        }
        return link;
    }





 有一个大集合List<User> big、一个小集合List<User> small、 从大集合中删除 小集合里的对象(带泛型)
 我上面写的方法不行、好像在判断对象是否重复有问题、
请问集合若是带了泛型 用set去重还可以么?


------解决方案--------------------
去重复可以,如果是自己写的对象要重写hashcode 和equals 方法。这样调用contains 能识别出来。祝你好运。