Skip to content
Related Articles

Related Articles

Merge Two Sets in Java

View Discussion
Improve Article
Save Article
  • Last Updated : 09 Dec, 2021
View Discussion
Improve Article
Save Article

The set interface is present in java.util package and extends the Collection interface is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the mathematical set. This interface contains the methods inherited from the Collection interface and adds a feature that restricts the insertion of the duplicate elements. There are two interfaces that extend the set implementation namely SortedSet and NavigableSet.

Methods: Following are the various ways to merge two sets in Java: 

  1. Using double brace initialization
  2. Using the addAll() method of the Set class
    • Using user-defined method
    • Using Java 8 stream in the user-defined function
  3. Using Java 8 stream in the user-defined function
  4. Using of() and forEach() Methods of Stream class
  5. Using of() and flatMap() Method of Stream class with Collector
  6. Using concat() method of Stream Class with Collector
  7. Using Apache Common Collections
  8. Using Guava Iterables.concat()
     

Method 1: Using Double brace Initialization

Illustration:

Input :  a = [1, 3, 5, 7, 9]
         b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example

Java




// Java Program to Demonstrate Merging of two sets in Java
// Using Double brace Initialization
  
// Importing required classes
import java.io.*;
import java.util.*;
import java.util.stream.*;
  
// Main class
public class GFG {
  
    // Method 1
    // To merge two sets
    // using DoubleBrace Initialisation
    public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
    {
  
        // Adding all elements of respective Sets
        // using addAll() method
        return new HashSet<T>() {
            {
                addAll(a);
                addAll(b);
            }
        };
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating the sets to be merged
  
        // First set
        Set<Integer> a = new HashSet<Integer>();
        // Applying Arrays.asList()
        a.addAll(
            Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
  
        // Second set
        Set<Integer> b = new HashSet<Integer>();
        // Applying Arrays.asList()
        b.addAll(
            Arrays.asList(new Integer[] { 0, 2, 4, 6, 8 }));
  
        // Printing the Sets
        System.out.println("Set a: " + a);
        System.out.println("Set b: " + b);
  
        // Calling Method 1 to merge above Sets
        System.out.println("Merged Set: " + mergeSet(a, b));
    }
}


Output: 

Set a: [1, 3, 5, 7, 9]
Set b: [0, 2, 4, 6, 8]
Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

Method 2: Using addAll() Method of Set Class

The addAll() method is provided by the Set interface. It adds the elements passed as parameters at the last of this set. 

2-A. Using user-defined method

Illustration:

Input  : a = [1, 3, 5, 7, 9]
         b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example:

Java




// Java program to demonstrate Merging of Two Sets
// Using SetAll() method
  
// Importing required classes
import java.util.*;
  
// Main class
public class GFG {
  
    // Method 1
    // To merge two sets
    // using addAll()
    public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
    {
  
        // Creating an empty HashSet
        Set<T> mergedSet = new HashSet<T>();
  
        // Adding the two sets to be merged
        // into the new Set using addAll() method
        mergedSet.addAll(a);
        mergedSet.addAll(b);
  
        // Returning the merged set
        return mergedSet;
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating the sets to be merged
  
        // First Set
        Set<Integer> a = new HashSet<Integer>();
        a.addAll(
            Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
  
        // Second Set
        Set<Integer> b = new HashSet<Integer>();
        b.addAll(
            Arrays.asList(new Integer[] { 0, 2, 4, 6, 8 }));
  
        // Printing the Sets
        System.out.println("Set a: " + a);
        System.out.println("Set b: " + b);
  
        // Calling method 1 to merge above Sets
        // and printing it
        System.out.println("Merged Set: " + mergeSet(a, b));
    }
}


Output: 

Set a: [1, 3, 5, 7, 9]
Set b: [0, 2, 4, 6, 8]
Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

2-B. Using Java 8 stream in the user-defined function

Illustration: 

Input : a = [1, 3, 5, 7, 9]
        b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example

Java




// Java program to demonstrate Merging of Two Sets
// Using Stream
  
// Importing required classes
import java.io.*;
import java.util.*;
import java.util.stream.*;
  
// Main class
public class GFG {
  
    // Method 1
    // To merge two Sets
    // using addAll()
    public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
    {
  
        // Creating a Set with 'a'
        Set<T> mergedSet
            = a.stream().collect(Collectors.toSet());
  
        // Adding the second set to be merged
        mergedSet.addAll(b);
  
        // Returning the merged Set
        return mergedSet;
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Creating the Sets to be merged
  
        // First set
        Set<Integer> a = new HashSet<Integer>();
        a.addAll(
            Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
  
        // Second set
        Set<Integer> b = new HashSet<Integer>();
        b.addAll(
            Arrays.asList(new Integer[] { 0, 2, 4, 6, 8 }));
  
        // Printing above Sets
        System.out.println("Set a: " + a);
        System.out.println("Set b: " + b);
  
        // Calling method 1 to merge two Sets
        System.out.println("Merged Set: " + mergeSet(a, b));
    }
}


Output: 

Set a: [1, 3, 5, 7, 9]
Set b: [0, 2, 4, 6, 8]
Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

Method 3:  Using addAll() method of Collections Class 

Illustration:

Input :   a = [1, 3, 5, 7, 9]
          b = [0, 2, 4, 6, 8]
Output :  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example:

Java




// Java Program to Merge Two Arrays
// of Same Type into an Object Array
  
// Importing required classes
import java.io.*;
import java.util.*;
  
// Main class
class GFG {
  
    // Method 1
    // To merging two Sets
    // using addAll()
    public static Set<Integer> mergeSet(Set<Integer> a,
                                        Set<Integer> b)
    {
  
        // Creating an empty HashSet of Integer type
        Set<Integer> mergedSet = new HashSet<>();
  
        // Adding the two sets to be merged
        // into the new Set
        Collections.addAll(mergedSet,
                           a.toArray(new Integer[0]));
        Collections.addAll(mergedSet,
                           b.toArray(new Integer[0]));
  
        // Returning the merged Set
        return mergedSet;
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating the sets to be merged
  
        // First set
        Set<Integer> a = new HashSet<Integer>();
        a.addAll(
            Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
  
        // Second set
        Set<Integer> b = new HashSet<Integer>();
        b.addAll(
            Arrays.asList(new Integer[] { 0, 2, 4, 6, 8 }));
  
        // Printing the above two Sets
        System.out.println("Set a: " + a);
        System.out.println("Set b: " + b);
  
        // Calling above method 1 to merge two sets
        System.out.println("Merged Set: " + mergeSet(a, b));
    }
}


Output: 

Set a: [1, 3, 5, 7, 9]
Set b: [0, 2, 4, 6, 8]
Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

Method 4: Using of() and forEach() Methods of Stream class 

Illustration: 

Input : a = [1, 3, 5, 7, 9]
        b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example:

Java




// Java Program to Demonstrate Merging of Two Sets
// Using Stream
  
// Importing required classes
import java.io.*;
import java.util.*;
import java.util.stream.*;
  
// Main class
public class GFG {
  
    // Method  1
    // To merge two sets
    // using Stream of() and forEach() methods
    public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
    {
  
        // Creating an empty set
        Set<T> mergedSet = new HashSet<T>();
  
        // add the two sets to be merged
        // into the new set
        Stream.of(a, b).forEach(mergedSet::addAll);
  
        // returning the merged set
        return mergedSet;
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating the sets to be merged
  
        // First set
        Set<Integer> a = new HashSet<Integer>();
        a.addAll(
            Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
  
        // Second set
        Set<Integer> b = new HashSet<Integer>();
        b.addAll(
            Arrays.asList(new Integer[] { 0, 2, 4, 6, 8 }));
  
        // Printing the above two Sets
        System.out.println("Set a: " + a);
        System.out.println("Set b: " + b);
  
        // Calling method 1 to merge two Sets
        System.out.println("Merged Set: " + mergeSet(a, b));
    }
}


Output: 

Set a: [1, 3, 5, 7, 9]
Set b: [0, 2, 4, 6, 8]
Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

Method 5: Using of() and flatMap() Method of Stream class with Collector

Illustration:

Input :  a = [1, 3, 5, 7, 9]
         b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example:

Java




// Java Program to Demonstrate Merging of Two Sets
// Using stream
  
// Importing required classes
import java.io.*;
import java.util.*;
import java.util.stream.*;
  
// Main class
public class GFG {
  
    // Method 1
    // To merge two Sets
    // using Stream of(), flatMap() and Collector
    public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
    {
  
        // Adding the two Sets to be merged
        // into the new Set and
        // returning the merged set
        return Stream.of(a, b)
            .flatMap(x -> x.stream())
            .collect(Collectors.toSet());
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Creating the sets to be merged
  
        // First Set
        Set<Integer> a = new HashSet<Integer>();
        a.addAll(
            Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
  
        // Second Set
        Set<Integer> b = new HashSet<Integer>();
        b.addAll(
            Arrays.asList(new Integer[] { 0, 2, 4, 6, 8 }));
  
        // Printing the sets
        System.out.println("Set a: " + a);
        System.out.println("Set b: " + b);
  
        // Calling method 1 to merge above two Sets
        System.out.println("Merged Set: " + mergeSet(a, b));
    }
}


Output: 

Set a: [1, 3, 5, 7, 9]
Set b: [0, 2, 4, 6, 8]
Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

Method 6: Using concat() method of Stream Class with Collector

Illustration: 

Input : a = [1, 3, 5, 7, 9]
        b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

The concatenate function is used to merge to string and make a single string that contains both the string. Stream.concat() method creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.

Example 

Java




// Java program to Demonstrate Merging of two Sets
// using Stream
  
// Importing required classes
import java.io.*;
import java.util.*;
import java.util.stream.*;
  
// Main class
public class GFG {
  
    // Method 1
    // To merge two sets
    // using Stream concat() and Collectors
    public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
    {
  
        // Adding the two sets to be merged
        // into the new Set and
        // returning the merged set
        return Stream.concat(a.stream(), b.stream())
            .collect(Collectors.toSet());
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Creating the sets to be merged
  
        // First Set
        Set<Integer> a = new HashSet<Integer>();
        a.addAll(
            Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
  
        // Second Set
        Set<Integer> b = new HashSet<Integer>();
        b.addAll(
            Arrays.asList(new Integer[] { 0, 2, 4, 6, 8 }));
  
        // Printing the above two Sets
        System.out.println("Set a: " + a);
        System.out.println("Set b: " + b);
  
        // Calling the method 1 to merge two Sets
        System.out.println("Merged Set: " + mergeSet(a, b));
    }
}


Output: 

Set a: [1, 3, 5, 7, 9]
Set b: [0, 2, 4, 6, 8]
Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

Method 7: Using Apache Common Collections

Illustration:

Input :  a = [1, 3, 5, 7, 9]
         b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example

Java




// Java Program to Demonstrate Merging of Two Sets
// Using Apache Common Collection
  
// Importing required classes
import java.io.*;
import java.util.*;
import org.apache.commons.collections4.SetUtils;
  
// Main class
public class GFG {
  
    // Method 1
    // To merge two Sets
    // using addAll() method
    public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
    {
  
        // Adding the two Sets to be merged
        // into the new Set and
        // returning the merged Set
        return SetUtils.union(a, b);
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating the Sets to be merged
  
        // First set
        Set<Integer> a = new HashSet<Integer>();
        a.addAll(
            Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
  
        // Second set
        Set<Integer> b = new HashSet<Integer>();
        b.addAll(
            Arrays.asList(new Integer[] { 0, 2, 4, 6, 8 }));
  
        // Printing the above two Sets
        System.out.println("Set a: " + a);
        System.out.println("Set b: " + b);
  
        // Calling method 1 to merge two Sets
        System.out.println("Merged Set: " + mergeSet(a, b));
    }
}


Output: 

Set a: [1, 3, 5, 7, 9]
Set b: [0, 2, 4, 6, 8]
Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

Method 8: Using Guava Iterables.concat()

Illustration:

Input : a = [1, 3, 5, 7, 9]
        b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example

Java




// Java Program to Demonstrate Merging of Two Sets
// Using Guava Library
  
// Importing required classes
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import java.io.*;
import java.util.*;
  
// Main class
public class GFG {
  
    // Method 1
    // To merge two sets
    // using Guava Iterables.concat()
    public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
    {
  
        // Adding the two sets to be merged
        // into the new set and
        // returning the merged set
        return Sets.newHashSet(Iterables.concat(a, b));
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating the Sets to be merged
  
        // First set
        Set<Integer> a = new HashSet<Integer>();
        a.addAll(
            Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
  
        // Second set
        Set<Integer> b = new HashSet<Integer>();
        b.addAll(
            Arrays.asList(new Integer[] { 0, 2, 4, 6, 8 }));
  
        // Printing the above two Sets
        System.out.println("Set a: " + a);
        System.out.println("Set b: " + b);
  
        // Calling method 1 to merge two Sets
        System.out.println("Merged Set: " + mergeSet(a, b));
    }
}


Output: 

Set a: [1, 3, 5, 7, 9]
Set b: [0, 2, 4, 6, 8]
Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

Note: Any duplicate element presented in the sets will be discarded during the merge in all the above methods.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!