sneppets-java8

Java 8 : Find union and intersection of two Lists (ArrayLists)

This tutorial shows you how to find union and intersection of two Lists (ArrayLists).

Union and Intersection of two Lists

package com.sneppets.example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class UnionIntersectionOfTwoListsExample {
	
	public static void main (String[] args) {
		
		List<String> list1 = new ArrayList<String>(Arrays.asList("a","b","c","d","e"));
		System.out.println("List 1 elements: " + list1);
		List<String> list2 = new ArrayList<String>(Arrays.asList("b","d","f","g"));
		System.out.println("List 2 elements: " + list2);
		
		System.out.println();
		System.out.println("Union.................");
		//Find union of two lists
		System.out.println("Union of List1 and List2 :" + getUnionOfLists(list1, list2));
		
		System.out.println();
		System.out.println("Intersection.................");
		//Find intersect of lists using Stream API (Java 8)
		System.out.println("Intersection of List1 & List2 Method 1: " + getIntersectOfLists1(list1, list2));	
				
		//Find intersect of lists using retainAll() method
		System.out.println("Intersection of List1 & List2 Method 2: " + getIntersectOfLists2(list1, list2));	
	}

	private static List<String> getUnionOfLists(List<String> list1, List<String> list2) {
		
		Set<String> set = new HashSet<>();
		set.addAll(list1);
		set.addAll(list2);
		
		return new ArrayList<>(set);
	}

	private static List<String> getIntersectOfLists1(List<String> list1, List<String> list2) {
		List<String> intersectElements = list1.stream()
				.filter(list2 :: contains)
				.collect(Collectors.toList());
		
		if(!intersectElements.isEmpty()) {
			return intersectElements;
		}else {
			return Collections.emptyList();
		}
	}

	private static List<String> getIntersectOfLists2(List<String> list1, List<String> list2) {
		list1.retainAll(list2);

		return list1;
	}

}

Output

List 1 elements: [a, b, c, d, e]
List 2 elements: [b, d, f, g]

Union.................
Union of List1 and List2 :[a, b, c, d, e, f, g]

Intersection.................
Intersection of List1 & List2 Method 1: [b, d]
Intersection of List1 & List2 Method 2: [b, d]

Union of two Lists

To find union of two lists you need to use addAll() method of Set interface and ArrayList constructor. You need to create new Set and add elements from list1 and list2 and construct new ArrayList using that set as shown.

private static List<String> getUnionOfLists(List<String> list1, List<String> list2) {
		
    Set<String> set = new HashSet<>();
    set.addAll(list1);
    set.addAll(list2);
		
    return new ArrayList<>(set);
}

Intersection of two Lists

1. Java 8 Stream API

List<String> intersectElements = list1.stream()
				.filter(list2 :: contains)
				.collect(Collectors.toList());

From Java 8 you can use sequential stream on list1 (bigger one) and filter it to produce a stream containing matching elements of specified collection (list2) and use Collectors.toList() to accumulate the intersects in to new List.

2. retainAll() method

list1.retainAll(list2);

The above code removes the elements from List1 that are not contained in the specified collection (List2).

Also See

References

guest
0 Comments
Inline Feedbacks
View all comments