find out the elements of an arraylist which is not present in another arraylist

JavaArraysListCollectionsArraylist

Java Problem Overview


I have to find a best way to find out that elements which is not presented in the second arraylist. suppose

Arraylist a,b, 

Arraylist a={1,2,3,4,5};
Arraylist b={2,3,4};

     

So basically what I want is to find out that elements of a which is not present in arraylist b.

So what is the best solutions to do that?

Java Solutions


Solution 1 - Java

List<Integer> c = new ArrayList<>(a);
c.removeAll(b);

Also consider to use Sets instead of Lists.

Solution 2 - Java

here is another approach using java 8 -

a.stream().filter(b::contains).collect(Collectors.toList());

Solution 3 - Java

You could use Apache Commons Collections, which has a method explicitly for this purpose:

public static void main(String[] args) {
	List<Integer> a = Arrays.asList(new Integer[] { 1, 2, 3, 4, 5 });
	List<Integer> b = Arrays.asList(new Integer[] { 2, 3, 4 });
	Collection<Integer> aMinusB = CollectionUtils.subtract(a, b);
	System.out.println(aMinusB);
}

The printed result is: [1, 5].

The Apache Commons libs are well tested and commonly used to extend standard Java functionalities. This particular method accepts Iterable as parameters, so you can use any Collection you want. You can also mix different collection types:

public static void main(String[] args) {
    List<Integer> a = Arrays.asList(new Integer[] { 1, 2, 3, 4, 5 });
    Set<Integer> b = new HashSet<Integer>(Arrays.asList(new Integer[] { 2, 3, 4 }));
    Collection<Integer> aMinusB = CollectionUtils.subtract(a, b);
    System.out.println(aMinusB);
}

The printed result is the same, [1, 5].

Check out the Javadoc here.


For sake of completeness, Google's Guava library does not have this feature:

>Collection *subtract*(Collection, Collection)
No equivalent--create an ArrayList containing a and then call remove on it for each element in b.

However, it implements a method called Sets.difference() method, which you could use if you prefer Guava and work with sets:

public static void main(String[] args) {
	Set<Integer> a = new HashSet<Integer>(Arrays.asList(new Integer[] { 1, 2, 3, 4, 5 }));
	Set<Integer> b = new HashSet<Integer>(Arrays.asList(new Integer[] { 2, 3, 4 }));
	Set<Integer> aMinusB = Sets.difference(a, b);
	System.out.println(aMinusB);
}

The result is all elements in a that doesn't exist in b (i.e. [1, 5] again). Of course, the order is not determined since it operates on sets.

Solution 4 - Java

You can try removeAll:

List<Integer> notPresent = new ArrayList<Integer>(a);
notPresent.removeAll(b);

Solution 5 - Java

Use org.apache.commons.collections4.ListUtils

Given

List<Integer> a = Arrays.asList(new Integer[]{  1,2,3,4,5});
List<Integer> b = Arrays.asList(new Integer[]{0,1,2,3});

Action

List<Integer> c = ListUtils.removeAll(b, a)

Result in List c

4, 5

Solution 6 - Java

Please try like this

for (Object o : a) {  
  if (!b.contains(o)) {  
    // this is not present
  }  
}  

Solution 7 - Java

Loop through one list, then check if each element in other list using contains.

Solution 8 - Java

Something like this. If you think there may be duplicates in a you can try another type of Collection, like a Set for notPresent.

   List<Integer> notPresent = new ArrayList<Integer>();
    
    for (Integer n : a){
     if (!b.contains(n)){
       notPresent.add(n);
     }
    }

Solution 9 - Java

Try this:

 public static void main(String[] args) {
        List<Integer> a = new ArrayList<Integer>();
        List<Integer> b = new ArrayList<Integer>();
        List<Integer> exclusion = new ArrayList<Integer>();

        a.add(1);
        a.add(2);
        a.add(3);
        a.add(4);

        b.add(1);
        b.add(2);
        b.add(3);
        b.add(5);

        for (Integer x : a) {
            if (!b.contains(x)) {
                exclusion.add(x);
            }
        }

        for (Integer x : exclusion) {
            System.out.println(x);
        }

    }

Solution 10 - Java

Try this...

Use the contains() method of List.

ArrayList<Integer> aList = new ArrayList<Integer>();

for (Integer i : a){

      if (!(b.contains(i))){

             aList.add(i);

       }

     else{
             continue;

      }

 }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionarvin_codeHunkView Question on Stackoverflow
Solution 1 - JavaPuceView Answer on Stackoverflow
Solution 2 - JavaRupView Answer on Stackoverflow
Solution 3 - JavaMagnilexView Answer on Stackoverflow
Solution 4 - JavaMikita BelahlazauView Answer on Stackoverflow
Solution 5 - JavamikaView Answer on Stackoverflow
Solution 6 - JavasunleoView Answer on Stackoverflow
Solution 7 - JavaNimChimpskyView Answer on Stackoverflow
Solution 8 - JavaAverroesView Answer on Stackoverflow
Solution 9 - JavaMawiaView Answer on Stackoverflow
Solution 10 - JavaKumar Vivek MitraView Answer on Stackoverflow