Java Top

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll learn how to retrieve the intersection of two Lists.

Like many other things, this has become much easier thanks to the introduction of streams in Java 8.

2. Intersection of Two Lists of Strings

Let’s create two Lists of Strings with some intersection — both having some duplicated elements:

List<String> list = Arrays.asList("red", "blue", "blue", "green", "red");
List<String> otherList = Arrays.asList("red", "green", "green", "yellow");

And now we’ll determine the intersection of the lists with the help of stream methods:

Set<String> result = list.stream()
  .distinct()
  .filter(otherList::contains)
  .collect(Collectors.toSet());

Set<String> commonElements = new HashSet(Arrays.asList("red", "green"));

Assert.assertEquals(commonElements, result);

First, we remove the duplicated elements with distinct. Then, we use the filter to select the elements that are also contained in the otherList.

Finally, we convert our output with a Collector. The intersection should contain each common element only once. The order shouldn’t matter, thus toSet is the most straightforward choice, but we can also use toList or another collector method.

For more details, review our guide to Java 8’s Collectors.

3. Intersection of Lists of Custom Classes

What if our Lists don’t contain Strings but rather instances of a custom class we’ve created? Well, as long as we follow Java’s conventions, the solution with stream methods will work fine for our custom class.

How does the contains method decide whether a specific object appears in a list? Based on the equals method. Thus, we have to override the equals method and make sure that it compares two objects based on the values of the relevant properties.

For instance, two rectangles are equal if their widths and heights are equal.

If we don’t override the equals method, our class uses the equals implementation of the parent class. At the end of the day, or rather, the inheritance chain, the Object class’ equals method gets executed. Then two instances are equal only if they refer to exactly the same object on the heap.

For more information about the equals method, see our article on Java equals() and hashCode() Contracts.

4. Conclusion

In this quick article, we’ve seen how to use streams to calculate the intersection of two lists. There are many other operations that used to be quite tedious but are pretty straightforward if we know our way around the Java Stream API. Take a look at our further tutorials with Java streams here.

Code examples are available over on GitHub.

Java bottom

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE LESSONS

2
Leave a Reply

avatar
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Eric MartinIlia Rodionov Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Ilia Rodionov
Guest
Ilia Rodionov

Why just not use List#retainAll?

Eric Martin
Member
Eric Martin

That is a good solution if it is ok to modify your original list. If not, you could definitely make a copy and then call List.retainAll().