0

I have multiple Integer ArrayList, which contains some duplicate elements. I want to get the unique elements from them. But how?
java.util.ArrayList.removeAll() is not serving my purpose completely. See the below test code-

ArrayList<Integer> d = new ArrayList<Integer>();
d.add(2);
d.add(4);
d.add(5);
d.add(7);
d.add(8);
d.add(9);

ArrayList<Integer> e = new ArrayList<Integer>();
e.add(3);
e.add(7);

d.removeAll(e);

for (int t : d) {
    System.out.print(t+", ");
}

In output, i am getting 2, 4, 5, 8, 9, . Clearly 3 is missing. Also just to keep it simple, I am using only two ArrayList here but in my code, i have more than two ArrayList.

How I can find unique elements in multiple ArrayList in Java

1
  • 1
    based on your code the output that you are getting is obvious. What I understand is as 7 is common, only 7 should be removed and 3 should be added... right? then use Set. It will not add duplicates... that way you will have all unique list... Commented Dec 8, 2012 at 13:20

6 Answers 6

3

Create a Set<Integer> from your List<Integer>. The set will contain no duplicate objects:

List<Integer> lstNumbers = new ArrayList<Integer>();
//fill the list of integers...
Set<Integer> setNumbers = new HashSet<Integer>(lstNumbers);
//the set will contain no duplicate values...
for (int t : setNumbers) {
    System.out.print(t+", ");
}

Note that you can add more List<Integer> in the Set by using the Set#addAll method (as shown in rahulroc answer) to add more integers in your not duplicated elements collection:

//assuming setNumbers has been initialized before
setNumbers.addAll(anotherListOfNumbers);
setNumbers.addAll(andAnotherListOfNumbers);

Also, as best practice, try to program to interfaces (List, Set, etc), not to class implementations (ArrayList, HashSet), as shown here: What does it mean to “program to an interface”?

Sign up to request clarification or add additional context in comments.

6 Comments

First, i need to add all the ArrayList, then i have to create HashSet. Am i right?
@RaviJoshi yes, first fill the ArrayList with duplicate elements, then create the Set.
@LuiggiMendoza .. great that you updated your answer based on my comment. I guess you missed this point from the question
@Muklas yes, this will work fine with Strings, basically because HashSet uses hashCode and equals methods, and String implements it. You can use your own class to check unique results as well, as long as you use HashSet and you class implements these methods as well.
|
2

use java.util.HashSet

Set<Integer> uniqueEntries = new HashSet<Integer>();
for(all lists)
     uniqueEntries.addAll(list);

Now the set uniqueEntries will contain all the unique integer values.

2 Comments

You can use a Collection<E> as parameter in the HashSet constructor.
Yes, we can. but he doesn't have just one arraylist. He needs to add multiple array lists. Hence, the constructor wont fulfil the entire purpose as he has to iterate over the whole list of all the arraylists.
2

Use Set

ArrayList<Integer> d = new ArrayList<Integer>();
d.add(2);
d.add(4);
d.add(5);
d.add(7);
d.add(8);
d.add(9);

d.add(3);
d.add(7);

// this will remove all duplicates
Set setNew = new HashSet(d);

System.out.print(setNew);

Demo

1 Comment

I like the DEMO most. You rock buddy. Thank you. I got it now.
0
 ArrayList<Integer> d = new ArrayList<Integer>();
        d.add(2);
        d.add(4);
        d.add(5);
        d.add(7);
        d.add(8);
        d.add(9);

        ArrayList<Integer> e = new ArrayList<Integer>();
        e.add(3);
        e.add(7);

        List<Integer> temp = new ArrayList<Integer>(d);

        System.out.println("d --> " + d);
        System.out.println("e --> " + e);

        d.removeAll(e);
        e.removeAll(temp);
        d.addAll(e);

        System.out.println("d --> " + d);

Comments

0
/* ArrayList Unique & Duplicate Records Print */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
ArrayList<Integer> d = new ArrayList<Integer>();
        d.add(2);
        d.add(4);
        d.add(5);
        d.add(7);
        d.add(8);
        d.add(9);

        ArrayList<Integer> e = new ArrayList<Integer>();
        e.add(3);
        e.add(7);

        List<Integer> temp = new ArrayList<Integer>(e);

        System.out.println("d --> " + d);
        System.out.println("e --> " + e);

        e.removeAll(d);
        temp.removeAll(e);

        System.out.println("e (unique)--> " + e);
        System.out.println("temp (duplicate) --> " + temp);
    }
}

Comments

0

You can do this much more simpler.

ArrayList first = new Arraylist<>; ArrayList second =new ArrayList<>;

   first.add("one"); 
   first.add("two");  
   first.add("three"); 
   second.add("two");  
   second.add("three");

For find the unique elements.. first.retainall(second);

Comments

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.