HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Array / How to get union between two arrays

How to get union between two arrays

Learn to find the union between two arrays in Java using HashSet class. In set theory, the union (denoted by U) of a collection of sets is the set of all elements in the collection.

For example, the union of two sets A and B is the set of all the elements which are either in A, or in B, or in both A and B.

Union
Union

To get the union of two arrays, follow these steps:

  1. Push first array in a HashSet instance.
  2. Use addAll() method to add the elements of the second array into set.
  3. Similarly, add all the elements of more arrays in the set, if any.

1. Union between two integer arrays

Java program to get the union between two integer arrays and print the output.

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

public class Main 
{
  public static void main(String[] args) 
  {
    Integer[] firstArray = {0,2,4,6,8};
      Integer[] secondArray = {1,3,5,7,9};
      
      HashSet<Integer> set = new HashSet<>(); 
      
      set.addAll(Arrays.asList(firstArray));
      
      set.addAll(Arrays.asList(secondArray));
      
      System.out.println(set);
    
    //convert to array
      Integer[] union = {};
    union = set.toArray(union);
    
    System.out.println(Arrays.toString(union));
  }
}

Program output.

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

2. Union between two string arrays

Java program to get the union between two string arrays and print the output.

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

public class Main 
{
  public static void main(String[] args) 
  {
    String[] firstArray = {"A", "B", "C", "D"};
    String[] secondArray = {"D", "A", "E", "F"};
    
    HashSet<String> set = new HashSet<>(); 
    
    set.addAll(Arrays.asList(firstArray));
    
    set.addAll(Arrays.asList(secondArray));
    
    System.out.println(set);
    
    //convert to array
    String[] union = {};
    union = set.toArray(union);
    
    System.out.println(Arrays.toString(union));
  }
}

Program output.

[A, B, C, D, E, F]
[A, B, C, D, E, F]

Happy Learning !!

Read More : How to get intersection between two arrays

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Array

  • Java – Array Introduction
  • Java – Print Array
  • Java – Print 2D Array
  • Java – Copy Array
  • Java – Copy Array Range
  • Java – Clone Array
  • Java – Array Deep Copy
  • Java – String to String[]
  • Java – byte[] to String
  • Java – String to byte[]
  • Java – Array Union
  • Java – Array Intersection
  • Array – Remove duplicate elements

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)