# Java - Union and Intersection of two lists

July 04, 2019

Suppose you have two lists, and you want Union and Intersection of those two lists.

## Example

``````Input:

list1: [1, 2, 3, 4]
list2: [3, 4, 5, 6]

union(list1, list2): [1, 2, 3, 4, 5, 6]
intersection(list1, list2): [3, 4]``````

## Solution

See the java code for multiple solutions:

``````public static List<Integer> getIntersection_1(List<Integer> l1, List<Integer> l2) {
return l1.stream().filter(l2::contains).collect(Collectors.toList());
}

public static List<Integer> getIntersection_2(List<Integer> l1, List<Integer> l2) {
Set<Integer> s1 = new HashSet<>(l1);
s1.retainAll(l2);
return new ArrayList<>(s1);
}

public static List<Integer> getIntersection_3(List<Integer> l1, List<Integer> l2) {
List<Integer> list = new ArrayList<Integer>();

for (Integer i : l1) {
if(l2.contains(i)) {
}
}

return list;
}

public static List<Integer> getUnion_1(List<Integer> l1, List<Integer> l2) {
Set<Integer> result = new HashSet<Integer>();

return new ArrayList<Integer>(result);
}

public static List<Integer> getUnion_2(List<Integer> l1, List<Integer> l2) {
Set<Integer> s1 = new HashSet<>(l1);
return new ArrayList<>(s1);
}``````

## Similar Posts

### Drupal 8: Bootstrap Sticky is not sticky in Drupal 8 - Solved

Bootstrap has a simple solution to have sticky blocks in your html. I’ve given a…

### A Practical Guide on how to work with Git Basic Commands and workflows

Introduction In this guide, we will see git basic commands, and fundamentals of…

### Kubernetes - How to Set Namespace So You Do Not Need to Mention it Again and Again in Kubectl Commands.

Introduction In some of the cases, we need to specify namespace name along with…

### Drupal 8 - How to hide a view block if content is empty

Introduction I have created a view, with some filters and content fields. I will…

### How to Protect Git branch and Enforce Restrictions

In previous article: Effective Git Branching Strategy, I discussed Git branching…

### Python Code - How To Read CSV into an Array of Arrays

Introduction In last post, we saw How to read CSV with Headers into Dictionary…

## Latest Posts

### Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

### How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

### How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

### Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

### Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

### Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Introduction Java log4j has many ways to initialize and append the desired…