A quick and programming guide to how to get the union of two unsorted arrays in java with example programs.
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.
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));
}
}
0 comments:
Post a Comment