Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Monday, October 5, 2020

Java Program To Get Union Of Two Arrays

1. Overview

In this article, you'll learn how to get the union of two arrays in java.  A union set is all the values of two sets or from all collection.

We can do the union function in java using HashSet with arrays. Use the addAll() method to add all the values of each array into HashSet.

This is a simple solution. As well as this solution will work with both numbers and string values.

Java Program To Get Union Of Two Arrays


2. Union of two Integer arrays with numbers

Let us write the java program to print the union of two integer arrays.

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

public class UnionTwoArraysNumbers {

	public static void main(String[] args) {

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

		// Integer array 2
		Integer[] array2 = { 2, 4, 6, 8, 10, 12, 14 };
		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));

		// adding the second array to set
		unionOfArrays.addAll(Arrays.asList(array2));

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

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

	}

}

Output:

Array 1 : [1, 2, 3, 4, 5, 6, 7, 8]
Array 2 : [2, 4, 6, 8, 10, 12, 14]
Union of two arrays: [1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14]

3. Union of two String arrays

Let us write the java program to print the union of two String arrays.


public class UnionTwoArraysStrings {

	public static void main(String[] args) {

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

		// Integer array 2
		String[] array2 = { "C", "D", "E", "F" };
		System.out.println("String  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));

		// adding the second array to set
		unionOfArrays.addAll(Arrays.asList(array2));

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

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

	}

}

Output:

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

4. Conclusion

In this article, we've seen how to find the union of two arrays in java using HashSet.

As usual, all examples. are over Github.

How to compare two strings?

How to add integers to ArrayList?

HashSet

String API Methods

Integer API

No comments:

Post a Comment

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