How to remove duplicate objects in a List without equals/hashcode?

后端 未结 21 1657
渐次进展
渐次进展 2020-12-03 02:53

I have to remove duplicated objects in a List. It is a List from the object Blog that looks like this:

public class Blog {
    private String title;
    priv         


        
21条回答
  •  没有蜡笔的小新
    2020-12-03 03:15

    use this code

     public List removeDuplicates(List list) {
        // Set set1 = new LinkedHashSet(list);
        Set set = new TreeSet(new Comparator() {
    
            @Override
            public int compare(Object o1, Object o2) {
                if (((Blog) o1).get().equalsIgnoreCase(((Blog) o2).getId()) /*&&
                        ((Blog)o1).getName().equalsIgnoreCase(((Blog)o2).getName())*/) {
                    return 0;
                }
                return 1;
            }
        });
        set.addAll(list);
    
        final List newList = new ArrayList(set);
        return newList;
    }
    

提交回复
热议问题