find out the elements of an arraylist which is not present in another arraylist
Solution 1
List<Integer> c = new ArrayList<>(a);
c.removeAll(b);
Also consider to use Sets instead of Lists.
Solution 2
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 3
here is another approach using java 8 -
a.stream().filter(b::contains).collect(Collectors.toList());
Solution 4
You can try removeAll
:
List<Integer> notPresent = new ArrayList<Integer>(a);
notPresent.removeAll(b);
Solution 5
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
Comments
-
arvin_codeHunk almost 2 years
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?
-
Puce over 11 years- Uses generics parameter not raw type - removeAll returns a boolean not a List
-
arvin_codeHunk over 11 yearsthis would be not a good practice, is there any kind of method which can sort and search automatically
-
Mikita Belahlazau over 11 yearsAs @Puce pointed, you better use sets instead of lists. They are faster for this kind of operations.
-
arvin_codeHunk over 11 yearswhat if I use Hashset ,like this hashset h; h.add(a); h.add(b); then iterate through this set
-
Mikita Belahlazau over 11 years@arvin_codeHunk if you need to keep all numbers sorted you can use TreeSet.
-
Klitos Kyriacou over 7 yearsAlthough 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.
-
nxh almost 7 yearsBe careful! removeAll() use equals() to compare elements. Therefore, you may not get the desired behavior.
-
Shirkam over 6 yearsI'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. -
bharal over 5 yearsi love that someone thought of the complete problem and made b have elements not present in a. thankyou.
-
Al-Mothafar over 4 yearsThis will find all items in a that exist in b, the question about finding items in a does NOT exist in b.
-
Reimeus about 4 years@Al-Mothafar
a.stream().filter(element -> !b.contains(element)).collect(Collectors.toList());
...