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

ravi
  • 6,140
  • 18
  • 77
  • 154
  • 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... – Fahim Parkar Dec 08 '12 at 13:20

6 Answers6

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”?

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • First, i need to add all the `ArrayList`, then i have to create `HashSet`. Am i right? – ravi Dec 08 '12 at 13:20
  • 1
    @RaviJoshi yes, first fill the `ArrayList` with duplicate elements, then create the `Set`. – Luiggi Mendoza Dec 08 '12 at 13:23
  • +1 for the link [What does it mean to program to an interface?](http://stackoverflow.com/q/383947/1065197) – ravi Dec 08 '12 at 13:24
  • @LuiggiMendoza .. great that you updated your answer based on my comment. I guess you missed this point from the question – Rahul Dec 08 '12 at 13:34
  • how about String, is like that too? – Muklas Jul 15 '19 at 23:11
  • 1
    @Muklas yes, this will work fine with `String`s, 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. – Luiggi Mendoza Jul 15 '19 at 23:25
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.

Rahul
  • 15,979
  • 4
  • 42
  • 63
  • You can use a `Collection` as parameter in the `HashSet` constructor. – Luiggi Mendoza Dec 08 '12 at 13:22
  • 1
    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. – Rahul Dec 08 '12 at 13:24
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

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • I like the [DEMO](http://ideone.com/3iOSS5) most. You rock buddy. Thank you. I got it now. – ravi Dec 08 '12 at 13:31
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);
Varun Verma
  • 406
  • 5
  • 5
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);
    }
}
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);

Raja Jawahar
  • 6,742
  • 9
  • 45
  • 56