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;
private String author;
private String url;
private String description;
...
}
A duplicated object is an object that have title, author, url and description equal to other object.
And I can’t alter the object. I can’t put new methods on it.
How do I do this?
If you can’t edit the source of the class (why not?), then you need to iterate over the list and compare each item based on the four criteria mentioned (“title, author, url and description”).
To do this in a performant way, I would create a new class, something like
BlogKeywhich contains those four elements and which properly implementsequals()andhashCode(). You can then iterate over the original list, constructing aBlogKeyfor each and adding to aHashMap:However the far simplest thing is to just edit the original source code of
Blogso that it correctly implementsequals()andhashCode().