Related Articles

Merge two sets in Java

• Last Updated : 11 Dec, 2018

Prerequisites : Sets in Java

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

1. Double brace Initialization :

Examples:

```Input : a = [1, 3, 5, 7, 9]
b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```
 `// Java program to demonstrate``// merging of two sets in Java``// using Double brace Initialization``import` `java.util.stream.*;``import` `java.util.*;``import` `java.io.*;`` ` `public` `class` `GfG {`` ` `    ``// Function merging two sets using DoubleBrace Initialisation``    ``public` `static` ` Set mergeSet(Set a, Set b)``    ``{``        ``return` `new` `HashSet() {{``                      ``addAll(a);``                       ``addAll(b);``        ``} };``    ``}`` ` `    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``// Creating the sets to be merged``        ``// First set``        ``Set a = ``new` `HashSet();``        ``a.addAll(Arrays.asList(``new` `Integer[] { ``1``, ``3``, ``5``, ``7``, ``9` `}));`` ` `        ``// Second set``        ``Set b = ``new` `HashSet();``        ``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 mergeSets()``        ``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. Set addAll() : The addAll() method is provided by the Set interface. It adds the elements passed as parameter at the last of this set.
• Using user-defined method

Examples:

```Input : a = [1, 3, 5, 7, 9]
b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```
 `// Java program to demonstrate``// merging of two sets in Java``// using SetAll() method``import` `java.util.*;`` ` `public` `class` `GfG {`` ` `    ``// Function merging two sets using addAll()``    ``public` `static` ` Set mergeSet(Set a, Set b)``    ``{`` ` `        ``// Creating an empty set``        ``Set mergedSet = ``new` `HashSet();`` ` `        ``// add the two sets to be merged``        ``// into the new set``        ``mergedSet.addAll(a);``        ``mergedSet.addAll(b);`` ` `        ``// returning the merged set``        ``return` `mergedSet;``    ``}`` ` `    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``// Creating the sets to be merged``        ``// First set``        ``Set a = ``new` `HashSet();``        ``a.addAll(Arrays.asList(``new` `Integer[] { ``1``, ``3``, ``5``, ``7``, ``9` `}));`` ` `        ``// Second set``        ``Set b = ``new` `HashSet();``        ``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 mergeSets()``        ``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]
```
• Using Java 8 stream in the user defined function

Examples:

```Input : a = [1, 3, 5, 7, 9]
b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```
 `// Java program to demonstrate``// merging of two sets in Java``// using stream`` ` `import` `java.util.stream.*;``import` `java.util.*;``import` `java.io.*;`` ` `public` `class` `GfG {`` ` `    ``// Function merging two sets using addAll()``    ``public` `static` ` Set mergeSet(Set a, Set b)``    ``{`` ` `        ``// Creating a set with 'a'``        ``Set mergedSet = a.stream()``                               ``.collect(Collectors.toSet());`` ` `        ``// add the second set to be merged``        ``mergedSet.addAll(b);`` ` `        ``// returning the merged set``        ``return` `mergedSet;``    ``}`` ` `    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``// Creating the sets to be merged``        ``// First set``        ``Set a = ``new` `HashSet();``        ``a.addAll(Arrays.asList(``new` `Integer[] { ``1``, ``3``, ``5``, ``7``, ``9` `}));`` ` `        ``// Second set``        ``Set b = ``new` `HashSet();``        ``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 mergeSets()``        ``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]
```

Examples:

```Input : a = [1, 3, 5, 7, 9]
b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```
 `// Java program to merge two arrays of``// same type into an Object array.`` ` `import` `java.util.*;``import` `java.io.*;`` ` `class` `GFG {`` ` `    ``// Function merging two sets using addAll()``    ``public` `static` `Set mergeSet(Set a, Set b)``    ``{`` ` `        ``// Creating an empty set``        ``Set mergedSet = ``new` `HashSet<>();`` ` `        ``// add 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;``    ``}`` ` `    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``// Creating the sets to be merged``        ``// First set``        ``Set a = ``new` `HashSet();``        ``a.addAll(Arrays.asList(``new` `Integer[] { ``1``, ``3``, ``5``, ``7``, ``9` `}));`` ` `        ``// Second set``        ``Set b = ``new` `HashSet();``        ``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 mergeSets()``        ``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]
```
4. Stream.of() + Stream.forEach():

Examples:

```Input : a = [1, 3, 5, 7, 9]
b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```
 `// Java program to demonstrate``// merging of two sets in Java``// using stream`` ` `import` `java.util.stream.*;``import` `java.util.*;``import` `java.io.*;`` ` `public` `class` `GfG {`` ` `    ``// Function merging two sets``    ``// using Stream of() and forEach() methods``    ``public` `static` ` Set mergeSet(Set a, Set b)``    ``{`` ` `        ``// Creating an empty set``        ``Set mergedSet = ``new` `HashSet();`` ` `        ``// add the two sets to be merged``        ``// into the new set``        ``Stream.of(a, b).forEach(mergedSet::addAll);`` ` `        ``// returning the merged set``        ``return` `mergedSet;``    ``}`` ` `    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``// Creating the sets to be merged``        ``// First set``        ``Set a = ``new` `HashSet();``        ``a.addAll(Arrays.asList(``new` `Integer[] { ``1``, ``3``, ``5``, ``7``, ``9` `}));`` ` `        ``// Second set``        ``Set b = ``new` `HashSet();``        ``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 mergeSets()``        ``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]
```
5. Stream.of() + flatMap() + Collector:

Examples:

```Input : a = [1, 3, 5, 7, 9]
b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```
 `// Java program to demonstrate``// merging of two sets in Java``// using stream`` ` `import` `java.util.stream.*;``import` `java.util.*;``import` `java.io.*;`` ` `public` `class` `GfG {`` ` `    ``// Function merging two sets``    ``// using Stream of(), flatMap() and Collector``    ``public` `static` ` Set mergeSet(Set a, Set b)``    ``{`` ` `        ``// add the two sets to be merged``        ``// into the new set and``        ``// return the merged set``        ``return` `Stream.of(a, b)``            ``.flatMap(x -> x.stream())``            ``.collect(Collectors.toSet());``    ``}`` ` `    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``// Creating the sets to be merged``        ``// First set``        ``Set a = ``new` `HashSet();``        ``a.addAll(Arrays.asList(``new` `Integer[] { ``1``, ``3``, ``5``, ``7``, ``9` `}));`` ` `        ``// Second set``        ``Set b = ``new` `HashSet();``        ``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 mergeSets()``        ``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]
```
6. Stream.concat() + Collector :

Examples:

```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.

 `// Java program to demonstrate``// merging of two sets in Java``// using stream`` ` `import` `java.util.stream.*;``import` `java.util.*;``import` `java.io.*;`` ` `public` `class` `GfG {`` ` `    ``// Function merging two sets``    ``// using Stream concat() and Collectors``    ``public` `static` ` Set mergeSet(Set a, Set b)``    ``{`` ` `        ``// add the two sets to be merged``        ``// into the new set and``        ``// return the merged set``        ``return` `Stream.concat(a.stream(), b.stream())``            ``.collect(Collectors.toSet());``    ``}`` ` `    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``// Creating the sets to be merged``        ``// First set``        ``Set a = ``new` `HashSet();``        ``a.addAll(Arrays.asList(``new` `Integer[] { ``1``, ``3``, ``5``, ``7``, ``9` `}));`` ` `        ``// Second set``        ``Set b = ``new` `HashSet();``        ``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 mergeSets()``        ``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]
```
7. Apache Common Collections:

Examples:

```Input : a = [1, 3, 5, 7, 9]
b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```
 `// Java program to demonstrate``// merging of two sets in Java``// using Apache Common Collection``import` `org.apache.commons.collections4.SetUtils;``import` `java.util.*;``import` `java.io.*;`` ` `public` `class` `GfG {`` ` `    ``// Function merging two sets using addAll()``    ``public` `static` ` Set mergeSet(Set a, Set b)``    ``{`` ` `        ``// add the two sets to be merged``        ``// into the new set and``        ``// return the merged set``        ``return` `SetUtils.union(a, b);``    ``}`` ` `    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``// Creating the sets to be merged``        ``// First set``        ``Set a = ``new` `HashSet();``        ``a.addAll(Arrays.asList(``new` `Integer[] { ``1``, ``3``, ``5``, ``7``, ``9` `}));`` ` `        ``// Second set``        ``Set b = ``new` `HashSet();``        ``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 mergeSets()``        ``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]
```
8. Guava Iterables.concat():

Examples:

```Input : a = [1, 3, 5, 7, 9]
b = [0, 2, 4, 6, 8]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```
 `// Java program to demonstrate``// merging of two sets in Java``// using Guava library`` ` `import` `com.google.common.collect.Iterables;``import` `com.google.common.collect.Sets;``import` `java.util.*;``import` `java.io.*;`` ` `public` `class` `GfG {`` ` `    ``// Function merging two sets``    ``// using Guava Iterables.concat()``    ``public` `static` ` Set mergeSet(Set a, Set b)``    ``{`` ` `        ``// add the two sets to be merged``        ``// into the new set and``        ``// return the merged set``        ``return` `Sets.newHashSet(Iterables.concat(a, b));``    ``}`` ` `    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``// Creating the sets to be merged``        ``// First set``        ``Set a = ``new` `HashSet();``        ``a.addAll(Arrays.asList(``new` `Integer[] { ``1``, ``3``, ``5``, ``7``, ``9` `}));`` ` `        ``// Second set``        ``Set b = ``new` `HashSet();``        ``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 mergeSets()``        ``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 presents in the sets will be discarded during the merge in all the above methods.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up