Pages

Wednesday, October 7, 2020

Java Program To Get Intersection Of Two Arrays

1. Overview

In this tutorial, you'll learn how to get the intersection of two arrays in java.  An Intersection Set is a common values among all collections. 

In our case, you need to get the values that are common in both arrays.

For example, Look at the below two arrays.

Array 1: { 1, 2, 3, 4, 5}

Array 2: {2,  4,  6,  8}

In these two arrays, 2 and 4 are the common values so these 2,4 set is an intersection of Array 1 and Array 2.

Java Program To Get Intersection Of Two Arrays


We are going to write the java program using HashSet and retainAll() method.

retainAll() Example to get unique and duplicate values from the list

We've seen in the previous article, how to get a union of two arrays of integers or string values.

2. Calculating the intersection of two integer arrays

Let us write a simple java program to get the common elements from two integer arrays.

package com.javaprogramto.programs.intersection;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * Java program to get the intersection of two arrays.
 * 
 * @author javaprogramto.com
 *
 */
public class IntersectionTwoArraysNumbers {

	public static void main(String[] args) {

		// Integer array 1
		Integer[] array1 = { 1, 2, 3, 4, 5 };
		System.out.println("Array 1 : " + Arrays.toString(array1));

		// Integer array 2
		Integer[] array2 = { 2, 4, 6, 8 };
		System.out.println("Array 2 : " + Arrays.toString(array2));

		// creating a new Set
		Set<Integer> unionOfArrays = new HashSet<>();

		// adding the first array to set
		unionOfArrays.addAll(Arrays.asList(array1));

		// Keeping the common values from array 2
		unionOfArrays.retainAll(Arrays.asList(array2));

		// converting set to array.
		Integer[] unionArray = {};
		unionArray = unionOfArrays.toArray(unionArray);

		// printing the union of two arrays.
		System.out.println("Inersection of two arrays of int numbers: " + Arrays.toString(unionArray));

	}

}

Output:

Array 1 : [1, 2, 3, 4, 5]
Array 2 : [2, 4, 6, 8]
Inersection of two arrays of int numbers: [2, 4]

3. Calculating the intersection of two string of arrays

Next, let us write the code to get the same intersection between a string of arrays.

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * Java program to get the intersection of two arrays with strings.
 * 
 * @author javaprogramto.com
 *
 */
public class IntersectionTwoArrayString {

	public static void main(String[] args) {

		// String array 1
		String[] array1 = { "A", "B", "C", "D" };
		System.out.println("Array 1 : " + Arrays.toString(array1));

		// String array 2
		String[] array2 = { "E", "A", "F", "B" };
		System.out.println("Array 2 : " + Arrays.toString(array2));

		// creating a new Set
		Set<String> unionOfArrays = new HashSet<>();

		// adding the first array to set
		unionOfArrays.addAll(Arrays.asList(array1));

		// Keeping the common values from array 2
		unionOfArrays.retainAll(Arrays.asList(array2));

		// converting set to array.
		String[] unionArray = {};
		unionArray = unionOfArrays.toArray(unionArray);

		// printing the union of two arrays.
		System.out.println("Inersection of two arrays of strings: " + Arrays.toString(unionArray));

	}

}

Output:

Array 1 : [A, B, C, D]
Array 2 : [E, A, F, B]
Inersection of two arrays of strings: [A, B]

4. Conclusion

In this article, you've seen how to get the intersection with numbers and string of arrays in java using HashSet.retainAll() method.

As usual, all the examples are shown over GitHub.

retainAll()

Intersection

No comments:

Post a Comment

Please do not add any spam links in the comments section.