74

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?

arvin_codeHunk
  • 2,328
  • 8
  • 32
  • 47
  • @arvin_codeHunk are the input arrays sorted by any means? – ppeterka Nov 08 '12 at 09:26
  • thanks for asking this question. I was asked this question today in an interview, although I was not able to answer it then, but its good to know the answer finally. – dgupta3091 May 31 '22 at 17:17

10 Answers10

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

Also consider to use Sets instead of Lists.

Puce
  • 37,247
  • 13
  • 80
  • 152
  • 17
    Be careful! removeAll() use equals() to compare elements. Therefore, you may not get the desired behavior. – nxh Jun 30 '17 at 03:59
21

here is another approach using java 8 -

a.stream().filter(b::contains).collect(Collectors.toList());
Rup
  • 243
  • 2
  • 2
  • 3
    I'd love this answer if it was more elaborated. For example, as @NamHoang says in another answer, this would use your `equals` method. Besides that, +1 for introducing new concepts in an old question. – Shirkam Jan 18 '18 at 08:19
  • 13
    This will find all items in a that exist in b, the question about finding items in a does NOT exist in b. – Al-Mothafar Oct 01 '19 at 07:35
  • 16
    @Al-Mothafar `a.stream().filter(element -> !b.contains(element)).collect(Collectors.toList());`... – Reimeus Apr 20 '20 at 15:31
15

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.

R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77
Magnilex
  • 11,584
  • 9
  • 62
  • 84
12

You can try removeAll:

List<Integer> notPresent = new ArrayList<Integer>(a);
notPresent.removeAll(b);
Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
10

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
mika
  • 2,495
  • 22
  • 30
  • 2
    Although you've dug up a four-year-old question, it's interesting to compare your solution, using Apache ListUtils, with Magnilex's solution using Apache CollectionUtils. Although they appear to do the same thing, their behaviour is subtly different - see their documentation. – Klitos Kyriacou Aug 30 '16 at 18:42
  • i love that someone thought of the complete problem and made b have elements not present in a. thankyou. – bharal Oct 29 '18 at 20:22
2

Please try like this

for (Object o : a) {  
  if (!b.contains(o)) {  
    // this is not present
  }  
}  
sunleo
  • 10,589
  • 35
  • 116
  • 196
1

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

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
1

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);
     }
    }
Averroes
  • 4,168
  • 6
  • 50
  • 63
1

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);
        }

    }
Mawia
  • 4,220
  • 13
  • 40
  • 55
0

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;

      }

 }
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75