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:
- Using double brace initialization
- Using the addAll() method of the Set class
- Using user-defined method
- Using Java 8 stream in the user-defined function
- Using Java 8 stream in the user-defined function
- Using of() and forEach() Methods of Stream class
- Using of() and flatMap() Method of Stream class with Collector
- Using concat() method of Stream Class with Collector
- Using Apache Common Collections
- 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
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class GFG {
public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
{
return new HashSet<T>() {
{
addAll(a);
addAll(b);
}
};
}
public static void main(String[] args)
{
Set<Integer> a = new HashSet<Integer>();
a.addAll(
Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 }));
Set<Integer> b = new HashSet<Integer>();
b.addAll(
Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 }));
System.out.println( "Set a: " + a);
System.out.println( "Set b: " + b);
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
import java.util.*;
public class GFG {
public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
{
Set<T> mergedSet = new HashSet<T>();
mergedSet.addAll(a);
mergedSet.addAll(b);
return mergedSet;
}
public static void main(String[] args)
{
Set<Integer> a = new HashSet<Integer>();
a.addAll(
Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 }));
Set<Integer> b = new HashSet<Integer>();
b.addAll(
Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 }));
System.out.println( "Set a: " + a);
System.out.println( "Set b: " + b);
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
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class GFG {
public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
{
Set<T> mergedSet
= a.stream().collect(Collectors.toSet());
mergedSet.addAll(b);
return mergedSet;
}
public static void main(String[] args)
{
Set<Integer> a = new HashSet<Integer>();
a.addAll(
Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 }));
Set<Integer> b = new HashSet<Integer>();
b.addAll(
Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 }));
System.out.println( "Set a: " + a);
System.out.println( "Set b: " + b);
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
import java.io.*;
import java.util.*;
class GFG {
public static Set<Integer> mergeSet(Set<Integer> a,
Set<Integer> b)
{
Set<Integer> mergedSet = new HashSet<>();
Collections.addAll(mergedSet,
a.toArray( new Integer[ 0 ]));
Collections.addAll(mergedSet,
b.toArray( new Integer[ 0 ]));
return mergedSet;
}
public static void main(String[] args)
{
Set<Integer> a = new HashSet<Integer>();
a.addAll(
Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 }));
Set<Integer> b = new HashSet<Integer>();
b.addAll(
Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 }));
System.out.println( "Set a: " + a);
System.out.println( "Set b: " + b);
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
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class GFG {
public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
{
Set<T> mergedSet = new HashSet<T>();
Stream.of(a, b).forEach(mergedSet::addAll);
return mergedSet;
}
public static void main(String[] args)
{
Set<Integer> a = new HashSet<Integer>();
a.addAll(
Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 }));
Set<Integer> b = new HashSet<Integer>();
b.addAll(
Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 }));
System.out.println( "Set a: " + a);
System.out.println( "Set b: " + b);
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
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class GFG {
public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
{
return Stream.of(a, b)
.flatMap(x -> x.stream())
.collect(Collectors.toSet());
}
public static void main(String[] args)
{
Set<Integer> a = new HashSet<Integer>();
a.addAll(
Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 }));
Set<Integer> b = new HashSet<Integer>();
b.addAll(
Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 }));
System.out.println( "Set a: " + a);
System.out.println( "Set b: " + b);
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
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class GFG {
public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
{
return Stream.concat(a.stream(), b.stream())
.collect(Collectors.toSet());
}
public static void main(String[] args)
{
Set<Integer> a = new HashSet<Integer>();
a.addAll(
Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 }));
Set<Integer> b = new HashSet<Integer>();
b.addAll(
Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 }));
System.out.println( "Set a: " + a);
System.out.println( "Set b: " + b);
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
import java.io.*;
import java.util.*;
import org.apache.commons.collections4.SetUtils;
public class GFG {
public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
{
return SetUtils.union(a, b);
}
public static void main(String[] args)
{
Set<Integer> a = new HashSet<Integer>();
a.addAll(
Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 }));
Set<Integer> b = new HashSet<Integer>();
b.addAll(
Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 }));
System.out.println( "Set a: " + a);
System.out.println( "Set b: " + b);
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
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import java.io.*;
import java.util.*;
public class GFG {
public static <T> Set<T> mergeSet(Set<T> a, Set<T> b)
{
return Sets.newHashSet(Iterables.concat(a, b));
}
public static void main(String[] args)
{
Set<Integer> a = new HashSet<Integer>();
a.addAll(
Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 }));
Set<Integer> b = new HashSet<Integer>();
b.addAll(
Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 }));
System.out.println( "Set a: " + a);
System.out.println( "Set b: " + b);
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.
Last Updated :
09 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...