Core Java

Java Program To Get Union Of Two Arrays

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.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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:

1
2
3
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.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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:

1
2
3
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

Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: Java Program To Get Union Of Two Arrays

Opinions expressed by Java Code Geeks contributors are their own.

Venkatesh Nukala

Venkatesh Nukala is a Software Engineer working for Online Payments Industry Leading company. In my free time, I would love to spend time with family and write articles on technical blogs. More on JavaProgramTo.com
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button