Merge two sets in Java
Prerequisites : Sets in Java
Following are the various ways to merge two sets in Java:
- 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 Initializationimportjava.util.stream.*;importjava.util.*;importjava.io.*;publicclassGfG {// Function merging two sets using DoubleBrace Initialisationpublicstatic<T> Set<T> mergeSet(Set<T> a, Set<T> b){returnnewHashSet<T>() {{addAll(a);addAll(b);} };}publicstaticvoidmain(String[] args){// Creating the sets to be merged// First setSet<Integer> a =newHashSet<Integer>();a.addAll(Arrays.asList(newInteger[] {1,3,5,7,9}));// Second setSet<Integer> b =newHashSet<Integer>();b.addAll(Arrays.asList(newInteger[] {0,2,4,6,8}));// Printing the setsSystem.out.println("Set a: "+ a);System.out.println("Set b: "+ b);// calling mergeSets()System.out.println("Merged Set: "+ mergeSet(a, b));}}chevron_rightfilter_noneOutput: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]
- 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() methodimportjava.util.*;publicclassGfG {// Function merging two sets using addAll()publicstatic<T> Set<T> mergeSet(Set<T> a, Set<T> b){// Creating an empty setSet<T> mergedSet =newHashSet<T>();// add the two sets to be merged// into the new setmergedSet.addAll(a);mergedSet.addAll(b);// returning the merged setreturnmergedSet;}publicstaticvoidmain(String[] args){// Creating the sets to be merged// First setSet<Integer> a =newHashSet<Integer>();a.addAll(Arrays.asList(newInteger[] {1,3,5,7,9}));// Second setSet<Integer> b =newHashSet<Integer>();b.addAll(Arrays.asList(newInteger[] {0,2,4,6,8}));// Printing the setsSystem.out.println("Set a: "+ a);System.out.println("Set b: "+ b);// calling mergeSets()System.out.println("Merged Set: "+ mergeSet(a, b));}}chevron_rightfilter_noneOutput: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 streamimportjava.util.stream.*;importjava.util.*;importjava.io.*;publicclassGfG {// Function merging two sets using addAll()publicstatic<T> Set<T> mergeSet(Set<T> a, Set<T> b){// Creating a set with 'a'Set<T> mergedSet = a.stream().collect(Collectors.toSet());// add the second set to be mergedmergedSet.addAll(b);// returning the merged setreturnmergedSet;}publicstaticvoidmain(String[] args){// Creating the sets to be merged// First setSet<Integer> a =newHashSet<Integer>();a.addAll(Arrays.asList(newInteger[] {1,3,5,7,9}));// Second setSet<Integer> b =newHashSet<Integer>();b.addAll(Arrays.asList(newInteger[] {0,2,4,6,8}));// Printing the setsSystem.out.println("Set a: "+ a);System.out.println("Set b: "+ b);// calling mergeSets()System.out.println("Merged Set: "+ mergeSet(a, b));}}chevron_rightfilter_noneOutput: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 user-defined method
- Collections.addAll() :
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.importjava.util.*;importjava.io.*;classGFG {// Function merging two sets using addAll()publicstaticSet<Integer> mergeSet(Set<Integer> a, Set<Integer> b){// Creating an empty setSet<Integer> mergedSet =newHashSet<>();// add the two sets to be merged// into the new setCollections.addAll(mergedSet, a.toArray(newInteger[0]));Collections.addAll(mergedSet, b.toArray(newInteger[0]));// returning the merged setreturnmergedSet;}publicstaticvoidmain(String[] args){// Creating the sets to be merged// First setSet<Integer> a =newHashSet<Integer>();a.addAll(Arrays.asList(newInteger[] {1,3,5,7,9}));// Second setSet<Integer> b =newHashSet<Integer>();b.addAll(Arrays.asList(newInteger[] {0,2,4,6,8}));// Printing the setsSystem.out.println("Set a: "+ a);System.out.println("Set b: "+ b);// calling mergeSets()System.out.println("Merged Set: "+ mergeSet(a, b));}}chevron_rightfilter_noneOutput: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]
- 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 streamimportjava.util.stream.*;importjava.util.*;importjava.io.*;publicclassGfG {// Function merging two sets// using Stream of() and forEach() methodspublicstatic<T> Set<T> mergeSet(Set<T> a, Set<T> b){// Creating an empty setSet<T> mergedSet =newHashSet<T>();// add the two sets to be merged// into the new setStream.of(a, b).forEach(mergedSet::addAll);// returning the merged setreturnmergedSet;}publicstaticvoidmain(String[] args){// Creating the sets to be merged// First setSet<Integer> a =newHashSet<Integer>();a.addAll(Arrays.asList(newInteger[] {1,3,5,7,9}));// Second setSet<Integer> b =newHashSet<Integer>();b.addAll(Arrays.asList(newInteger[] {0,2,4,6,8}));// Printing the setsSystem.out.println("Set a: "+ a);System.out.println("Set b: "+ b);// calling mergeSets()System.out.println("Merged Set: "+ mergeSet(a, b));}}chevron_rightfilter_noneOutput: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]
- 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 streamimportjava.util.stream.*;importjava.util.*;importjava.io.*;publicclassGfG {// Function merging two sets// using Stream of(), flatMap() and Collectorpublicstatic<T> Set<T> mergeSet(Set<T> a, Set<T> b){// add the two sets to be merged// into the new set and// return the merged setreturnStream.of(a, b).flatMap(x -> x.stream()).collect(Collectors.toSet());}publicstaticvoidmain(String[] args){// Creating the sets to be merged// First setSet<Integer> a =newHashSet<Integer>();a.addAll(Arrays.asList(newInteger[] {1,3,5,7,9}));// Second setSet<Integer> b =newHashSet<Integer>();b.addAll(Arrays.asList(newInteger[] {0,2,4,6,8}));// Printing the setsSystem.out.println("Set a: "+ a);System.out.println("Set b: "+ b);// calling mergeSets()System.out.println("Merged Set: "+ mergeSet(a, b));}}chevron_rightfilter_noneOutput: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]
- 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 streamimportjava.util.stream.*;importjava.util.*;importjava.io.*;publicclassGfG {// Function merging two sets// using Stream concat() and Collectorspublicstatic<T> Set<T> mergeSet(Set<T> a, Set<T> b){// add the two sets to be merged// into the new set and// return the merged setreturnStream.concat(a.stream(), b.stream()).collect(Collectors.toSet());}publicstaticvoidmain(String[] args){// Creating the sets to be merged// First setSet<Integer> a =newHashSet<Integer>();a.addAll(Arrays.asList(newInteger[] {1,3,5,7,9}));// Second setSet<Integer> b =newHashSet<Integer>();b.addAll(Arrays.asList(newInteger[] {0,2,4,6,8}));// Printing the setsSystem.out.println("Set a: "+ a);System.out.println("Set b: "+ b);// calling mergeSets()System.out.println("Merged Set: "+ mergeSet(a, b));}}chevron_rightfilter_noneOutput: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]
- 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 Collectionimportorg.apache.commons.collections4.SetUtils;importjava.util.*;importjava.io.*;publicclassGfG {// Function merging two sets using addAll()publicstatic<T> Set<T> mergeSet(Set<T> a, Set<T> b){// add the two sets to be merged// into the new set and// return the merged setreturnSetUtils.union(a, b);}publicstaticvoidmain(String[] args){// Creating the sets to be merged// First setSet<Integer> a =newHashSet<Integer>();a.addAll(Arrays.asList(newInteger[] {1,3,5,7,9}));// Second setSet<Integer> b =newHashSet<Integer>();b.addAll(Arrays.asList(newInteger[] {0,2,4,6,8}));// Printing the setsSystem.out.println("Set a: "+ a);System.out.println("Set b: "+ b);// calling mergeSets()System.out.println("Merged Set: "+ mergeSet(a, b));}}chevron_rightfilter_noneOutput: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]
- 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 libraryimportcom.google.common.collect.Iterables;importcom.google.common.collect.Sets;importjava.util.*;importjava.io.*;publicclassGfG {// Function merging two sets// using Guava Iterables.concat()publicstatic<T> Set<T> mergeSet(Set<T> a, Set<T> b){// add the two sets to be merged// into the new set and// return the merged setreturnSets.newHashSet(Iterables.concat(a, b));}publicstaticvoidmain(String[] args){// Creating the sets to be merged// First setSet<Integer> a =newHashSet<Integer>();a.addAll(Arrays.asList(newInteger[] {1,3,5,7,9}));// Second setSet<Integer> b =newHashSet<Integer>();b.addAll(Arrays.asList(newInteger[] {0,2,4,6,8}));// Printing the setsSystem.out.println("Set a: "+ a);System.out.println("Set b: "+ b);// calling mergeSets()System.out.println("Merged Set: "+ mergeSet(a, b));}}chevron_rightfilter_noneOutput: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.
Recommended Posts:
- Sets intersection() function | Guava | Java
- Sets union() function | Guava | Java
- Sets powerSet() function | Guava | Java
- Sets difference() function | Guava | Java
- StringJoiner merge() method in Java
- Merge arrays into a new object array in Java
- Java program to merge two files into a third file
- Java program to merge two files alternatively into third file
- Java program to merge contents of all the files in a directory
- Java program to merge two files alternatively into third file
- HashMap merge(key, value, BiFunction) method in Java with Examples
- How to merge two PHP objects?
- ES6 | Merge Objects
- Merge Strategies in Git
- How to merge the duplicate value in multidimensional array in PHP ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

