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

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

    Here is one way of removing a duplicate object.

    The blog class should be something like this or similar, like proper pojo

    public class Blog {
    
        private String title;
        private String author;
        private String url;
        private String description;
    
        private int hashCode;
    
    
    
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
           this.title = title;
        }
        public String getAuthor() {
            return author;
        }
        public void setAuthor(String author) {
            this.author = author;
        }
        public String getUrl() {
            return url;
        }
        public void setUrl(String url) {
            this.url = url;
        }
        public String getDescription() {
            return description;
        }
        public void setDescription(String description) {
            this.description = description;
        }
    
        @Override
        public boolean equals(Object obj) {
    
            Blog blog = (Blog)obj;
    
            if(title.equals(blog.title) &&
                    author.equals(blog.author) &&
                    url.equals(blog.url) &&
                    description.equals(blog.description))
            {
                hashCode = blog.hashCode;
                return true;
            }else{
                hashCode = super.hashCode();
                return false;
            }
        }
    
    }
    

    And use it like this to remove duplicates objects. The key data structure here is the Set and LinkedHashSet. It will remove duplicates and also keep order of entry

        Blog blog1 = new Blog();
        blog1.setTitle("Game of Thrones");
        blog1.setAuthor("HBO");
        blog1.setDescription("The best TV show in the US");
        blog1.setUrl("www.hbonow.com/gameofthrones");
    
        Blog blog2 = new Blog();
        blog2.setTitle("Game of Thrones");
        blog2.setAuthor("HBO");
        blog2.setDescription("The best TV show in the US");
        blog2.setUrl("www.hbonow.com/gameofthrones");
    
        Blog blog3 = new Blog();
        blog3.setTitle("Ray Donovan");
        blog3.setAuthor("Showtime");
        blog3.setDescription("The second best TV show in the US");
        blog3.setUrl("www.showtime.com/raydonovan");
    
        ArrayList listOfBlogs = new ArrayList<>();
    
        listOfBlogs.add(blog1);
        listOfBlogs.add(blog2);
        listOfBlogs.add(blog3);
    
    
        Set setOfBlogs = new LinkedHashSet<>(listOfBlogs);
    
        listOfBlogs.clear();
        listOfBlogs.addAll(setOfBlogs);
    
        for(int i=0;i

    Running this should print

    Game of Thrones
    Ray Donovan
    

    The second one will be removed because it is a duplicate of the first object.

提交回复
热议问题