# 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

### How to Patch and Build Python 3.9.x for FIPS enabled Openssl

Introduction In this post, we will see Python 3.9.x patch for FIPS enabled…

### Python - How to Implement Timed-Function which gets Timeout After Specified Max Timeout Value

Introduction We often require to execute in timed manner, i.e. to specify a max…

### Azure Storage Blob - How to List and Download Blob from Azure Storage container in Python (No Azure library)

Introduction In this tutorial we will see, How to list and download storage…

### Python - How to apply patch to Python and Install Python via Pyenv

Introduction In this post, we will see how we can apply a patch to Python and…

### php55w-common conflicts with php-common-5.* | Php issues while installing libraries

I was trying to install mongo extension with pecl. It gave me error: Then, I…

### Lets Encrypt SSL Error&#58; The client lacks sufficient authorization 403 Forbidden

This is due to our web server are configured to deny accessing this directory…

## Latest Posts

### Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

### Python SMTP Email Code - Sender Address Rejected - Not Owned By User

Introduction In a normal email sending code from python, I’m getting following…

### Nodejs with MongoDB - Number of Opened Connections Keep on Increasing with Mongoose Library

Introduction In one of my app, I was using to talk to . I have used some event…

### Django Python - How to Build Docker Image and Run Web-service on Apache with Python 3.9

Introduction So you have a Django project, and want to run it using docker image…

### Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

### Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…