Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 862
arvin_codeHunk Avatar asked Nov 08 '12 09:11

arvin_codeHunk


People also ask

How do you check if an ArrayList contains another ArrayList?

ArrayList contains() method in Java is used for checking if the specified element exists in the given list or not. Returns: It returns true if the specified element is found in the list else it returns false.

How do you check if an element is present in ArrayList or not?

ArrayList. contains() method can be used to check if an element exists in an ArrayList or not. This method has a single parameter i.e. the element whose presence in the ArrayList is tested. Also it returns true if the element is present in the ArrayList and false if the element is not present.

How do you check if all items in a list are in another list Java?

The containsAll() method of List interface in Java is used to check if this List contains all of the elements in the specified Collection. So basically it is used to check if a List contains a set of elements or not.


1 Answers

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

Also consider to use Sets instead of Lists.

like image 121
Puce Avatar answered Sep 28 '22 00:09

Puce