Java 8 How to get Common Elements From Two Lists

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Here we will see how to get common elements from two lists we can even say the intersection of two lists using different ways.
Input:
a = {"1","2","3","4","5"}
b = {"1","2","3"}

Result: 1,2,3
retainAll() method from the Collection interface is used to remove the common elements from two different lists.
List<Integer> list1 = new ArrayList<>(Arrays.asList(1,2,3,4,5));
List<Integer> list2 = new ArrayList<>(Arrays.asList(1,2,3));
list1.retainAll(list2);
System.out.println(list1);
Output:
[1, 2, 3]
We even get the common elements from two lists using java 8 stream API distinct() method.
List<Integer> list1 = new ArrayList<>(Arrays.asList(1,2,3,4,5));
List<Integer> list2 = new ArrayList<>(Arrays.asList(1,2,3));

List<Integer> result = list1.stream()
        .distinct()
        .filter(list2::contains)
        .collect(Collectors.toList());

result.forEach(System.out::print);
Output:
123
Getting common elements from two different lists using java 8 filter condition.
List<Integer> list1 = new ArrayList<>(Arrays.asList(1,2,3,4,5));
List<Integer> list2 = new ArrayList<>(Arrays.asList(1,2,3));

List<Integer> finalList =  list1.stream().filter(item-> !list2.contains(item)).collect(Collectors.toList());

finalList.stream().forEach(System.out::print);
Output:
123
Done!
Java 8 Stream API
Happy Learning 🙂

ArrayList in Java
Java 8 Stream Filter Example with Objects
Java 8 Getting Min and Max values from a Stream
Java 8 Stream API and Parallelism
How to get Stream count in Java 8
How to Merge Streams in Java 8
Java 8 How to Convert List to String comma separated values
Python List Data Structure In Depth
2 ways to Create ArrayList in Java
How to calculate Employees Salaries Java 8 summingInt
How to Convert Iterable to Stream Java 8
How to add elements to ArrayList in Java
How to merge two lists in Python
jQuery Add Elements Example Tutorials
JQuery Remove Elements Example

ArrayList in Java
Java 8 Stream Filter Example with Objects
Java 8 Getting Min and Max values from a Stream
Java 8 Stream API and Parallelism
How to get Stream count in Java 8
How to Merge Streams in Java 8
Java 8 How to Convert List to String comma separated values
Python List Data Structure In Depth
2 ways to Create ArrayList in Java
How to calculate Employees Salaries Java 8 summingInt
How to Convert Iterable to Stream Java 8
How to add elements to ArrayList in Java
How to merge two lists in Python
jQuery Add Elements Example Tutorials
JQuery Remove Elements Example






Varun
March 16, 2022 at 2:32 am - Reply 

How can I do this for n number of lists?









shree Dhar
March 19, 2022 at 6:47 am - Reply 

For Common Elements from two lists using java8 filter:
filter is not using properly.
Current query – list1.stream().filter(item-> !list2.contains(item)).collect(Collectors.toList());
Correct query – list1.stream().filter(item-> list2.contains(item)).collect(Collectors.toList());










Varun
March 16, 2022 at 2:32 am - Reply 

How can I do this for n number of lists?




How can I do this for n number of lists?





shree Dhar
March 19, 2022 at 6:47 am - Reply 

For Common Elements from two lists using java8 filter:
filter is not using properly.
Current query – list1.stream().filter(item-> !list2.contains(item)).collect(Collectors.toList());
Correct query – list1.stream().filter(item-> list2.contains(item)).collect(Collectors.toList());




For Common Elements from two lists using java8 filter:
filter is not using properly.
Current query – list1.stream().filter(item-> !list2.contains(item)).collect(Collectors.toList());
Correct query – list1.stream().filter(item-> list2.contains(item)).collect(Collectors.toList());
 


Δ
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String