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
We can also use Comparator to check duplicate elements. Sample code is given below,
private boolean checkDuplicate(List studentDTOs){
Comparator<StudentDTO > studentCmp = ( obj1, obj2)
->{
if(obj1.getName().equalsIgnoreCase(obj2.getName())
&& obj1.getAddress().equalsIgnoreCase(obj2.getAddress())
&& obj1.getDateOfBrith().equals(obj2.getDateOfBrith())) {
return 0;
}
return 1;
};
Set<StudentDTO> setObj = new TreeSet<>(studentCmp);
setObj.addAll(studentDTOs);
return setObj.size()==studentDTOs.size();
}
Use set:
yourList = new ArrayList<Blog>(new LinkedHashSet<Blog>(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.
And i can't alter the object. I can't put new methods on it.
How do i do this?
In case you also mean how do I make the object immutable and prevent subclassing: use the final
keyword
public final class Blog { //final classes can't be extended/subclassed
private final String title; //final members have to be set in the constructor and can't be changed
private final String author;
private final String url;
private final String description;
...
}
Edit: I just saw some of your comments and it seems you want to change the class but can't (third party I assume).
To prevent duplicates you might use a wrapper that implements appropriate equals()
and hashCode()
, then use the Set
aproach mentioned by the others:
class BlogWrapper {
private Blog blog; //set via constructor etc.
public int hashCode() {
int hashCode = blog.getTitle().hashCode(); //check for null etc.
//add the other hash codes as well
return hashCode;
}
public boolean equals(Object other) {
//check if both are BlogWrappers
//remember to check for null too!
Blog otherBlog = ((BlogWrapper)other).getBlog();
if( !blog.getTitle().equals(otherBlog.getTitle()) {
return false;
}
... //check other fields as well
return true
}
}
Note that this is just a rough and simple version and doesn't contain the obligatory null checks.
Finally use a Set<BlogWrapper>
, loop through all the blogs and try to add new BlogWrapper(blog)
to the set. In the end, you should only have unique (wrapped) blogs in the set.
use this code
public List<Blog> removeDuplicates(List<Blog> list) {
// Set set1 = new LinkedHashSet(list);
Set set = new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (((Blog) o1).get().equalsIgnoreCase(((Blog) o2).getId()) /*&&
((Blog)o1).getName().equalsIgnoreCase(((Blog)o2).getName())*/) {
return 0;
}
return 1;
}
});
set.addAll(list);
final List newList = new ArrayList(set);
return newList;
}
The easiest and the most effective way would be to allow eclipse to generate and override the equals and hashcode method. Just select the attributes to be checked for duplicates when prompted and you should be all set.
Also once the list is ready, put it into a Set and you have the duplicates gone.
This can be logically solved using a property. Here I have a property called a key.
List<Object> objectList = new ArrayList<>();
List<String> keyList = new ArrayList<>();
objectList.forEach( obj -> {
if(keyList.contains(unAvailabilityModel.getKey()))
objectList.remove(unAvailabilityModel);
else
keyList.add(unAvailabilityModel.getKey();
});
return objectList;