0

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
  • Did you override equals in your "Person" class? Removing duplicates like suggested in this post stackoverflow.com/a/14361428/966852 should work once equals is implemented. Commented May 12, 2016 at 5:56
  • @Shanu : You can use HashSet or any collection that implements set interface..!! Commented May 12, 2016 at 5:56

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

5 Comments

It is taking around 1 minute though the list is getting eliminated all the duplicates.
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.
I will try with set then. No I am not using a LinkedHashSet. Should I.?
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.
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
0

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.