Hi I have two array list List l1 and List l2, Name,age,sex,uniqID,marks are the data inside the lists. I want to join l1 and l2 without duplicates.uniqID is unique for each student. I have been looping through all the elements and comparing each and every element. But My list would have around 20k items so the looping is taking too much time. I have tried all the answers from this post nothing worked for me. Any suggestions.?
2 Answers
Simple example:
public class Person{
int id;
String name;
//fields, getter, setter, constructor omited....
@Override
public boolean equals(Object o){
if (!(o instanceof Person)){
//implicit null check
return false;
}
return this.id==((Person)o).id;
}
@Override
public int hashCode(){
return this.id;
}
}
The class Person does implement equals and hashCode now. equals is used by java for deciding if an object is a duplicate of another object. hashCode is not necessary per se, but it is suggested to override hashCode and equals together.
If both methods are implemented, you can simply use the build-in methods and datastructures in java:
With lists:
List<Person> listA = new ArrayList<>();
List<Person> listB = new ArrayList<>();
// filling the lists omitted
List<Person> mergedList=new ArrayList<>();
mergedList.addAll(listA);
mergedList.removeAll(listB);
mergedList.addAll(listB);
Or with sets:
List<Person> listA = new ArrayList<>();
List<Person> listB = new ArrayList<>();
// filling the lists omitted
Set<Person> mergedSet=new HashSet<>();
mergedSet.addAll(listA);
mergedSet.addAll(listB);
5 Comments
Sreyas
It is taking around 1 minute though the list is getting eliminated all the duplicates.
samjaf
Which example did you use? The solution using
Set should perform a lot better than the solution using List. If you still need the order of the list after merging, you could use a LinkedHashSet.Sreyas
I will try with set then. No I am not using a LinkedHashSet. Should I.?
samjaf
The List has a specific order. A regular Set has no information, which element was added first. Iterating over the elements of a set may return the elements in any order. The
LinkedHashSet on the other hand will return the elements in the same order in which the elements were added to it. So if the order of the elements inside the two lists is relevant after merging, you should use the LinkedHashSet. In any other situation, a regular HashSet will suffice.Sreyas
I used the second one. Once I eliminated the duplicate I converted the set to an array list then I have sorted the list for final use. Do you have any other suggestions.? Thanks a lot
You can combine both the arraylist and pass it to HashSet object As Set does not contain duplicates you can do following
ArrayList<String> a=new ArrayList<String>();
ArrayList<String> b=new ArrayList<String>();
b.addAll(a);
If you want to retain the order of elements use LinkedHashSet
LinkedHashSet<String> result=new LinkedHashSet<String>(b);
1 Comment
samjaf
combining both lists and adding them to the
Set is more work than necessary. You can just call addAll both lists to the (empty) Set. Furthermore the problem is the absence of equals and hashCode in the custom class which will prevent the Set from working properly.
equalsin your "Person" class? Removing duplicates like suggested in this post stackoverflow.com/a/14361428/966852 should work onceequalsis implemented.HashSetor any collection that implementssetinterface..!!