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

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

    Use set:

    yourList = new ArrayList(new LinkedHashSet(yourList));

    This will create list without duplicates and the element order will be as in original list.

    Just do not forget to implement hashCode() and equals() for your class Blog.

提交回复
热议问题