如何在没有等于/哈希码的 List<MyObject> 中删除重复的对象?

新手上路,请多包涵

我必须删除列表中的重复对象。它是对象博客中的一个列表,如下所示:

 public class Blog {
    private String title;
    private String author;
    private String url;
    private String description;
    ...
}

重复对象是具有与其他对象相同的标题、作者、url 和描述的对象。

而且我无法更改对象。我不能把新方法放在上面。

我该怎么做呢?

原文由 Diego Faria 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 618
2 个回答

如果您无法编辑类的源代码(为什么不呢?),那么您需要遍历列表并根据提到的四个标准(“标题、作者、网址和描述”)比较每个项目。

为了以高效的方式执行此操作,我将创建一个新类,例如 BlogKey 它包含这四个元素并 正确实现 equals()hashCode() 然后,您可以遍历原始列表,为每个列表构造一个 BlogKey 并添加到一个 HashMap

 Map<BlogKey, Blog> map = new HashMap<BlogKey, Blog>();
for (Blog blog : blogs) {
     BlogKey key = createKey(blog);
     if (!map.containsKey(key)) {
          map.put(key, blog);
     }
}
Collection<Blog> uniqueBlogs = map.values();

然而,最简单的方法是编辑 Blog 的原始源代码,以便正确实现 equals()hashCode()

原文由 matt b 发布,翻译遵循 CC BY-SA 3.0 许可协议

以下是适用于此场景的完整代码:

 class Blog {
    private String title;
    private String author;
    private String url;
    private String description;

    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;
    }

    Blog(String title, String author, String url, String description)
    {
        this.title = title;
        this.author = author;
        this.url = url;
        this.description = description;
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        if(obj instanceof Blog)
        {
            Blog temp = (Blog) obj;
            if(this.title.equals(temp.title) && this.author.equals(temp.author) && this.url.equals(temp.url) && this.description.equals(temp.description))
                return true;
        }
        return false;
    }

    @Override
    public int hashCode() {
        // TODO Auto-generated method stub

        return (this.title.hashCode() + this.author.hashCode() + this.url.hashCode() + this.description.hashCode());
    }
}

这是将消除重复项的主要功能:

 public static void main(String[] args) {
    Blog b1 = new Blog("A", "sam", "a", "desc");
    Blog b2 = new Blog("B", "ram", "b", "desc");
    Blog b3 = new Blog("C", "cam", "c", "desc");
    Blog b4 = new Blog("A", "sam", "a", "desc");
    Blog b5 = new Blog("D", "dam", "d", "desc");
    List<Blog> list = new ArrayList();
    list.add(b1);
    list.add(b2);
    list.add(b3);
    list.add(b4);
    list.add(b5);

    //Removing Duplicates;
    Set<Blog> s= new HashSet<Blog>();
    s.addAll(list);
    list = new ArrayList<Blog>();
    list.addAll(s);
    //Now the List has only the identical Elements
}

原文由 Sandeep 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题