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

后端 未结 21 1607
渐次进展
渐次进展 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:13

    We can also use Comparator to check duplicate elements. Sample code is given below,

    private boolean checkDuplicate(List studentDTOs){

        Comparator studentCmp = ( obj1,  obj2)
                ->{
            if(obj1.getName().equalsIgnoreCase(obj2.getName())
                    && obj1.getAddress().equalsIgnoreCase(obj2.getAddress())
                    && obj1.getDateOfBrith().equals(obj2.getDateOfBrith())) {
                return 0;
            }
            return 1;
        };
        Set setObj = new TreeSet<>(studentCmp);
        setObj.addAll(studentDTOs);
        return setObj.size()==studentDTOs.size();
    }
    

提交回复
热议问题