You can do it using this one-liner

Set<String> combined = Stream.concat(newStringSet.stream(), oldStringSet.stream())
        .collect(Collectors.toSet());

With a static import it looks even nicer

Set<String> combined = concat(newStringSet.stream(), oldStringSet.stream())
        .collect(toSet());

Another way is to use flatMap method:

Set<String> combined = Stream.of(newStringSet, oldStringSet).flatMap(Set::stream)
        .collect(toSet());

Also any collection could easily be combined with a single element

Set<String> combined = concat(newStringSet.stream(), Stream.of(singleValue))
        .collect(toSet());
Answer from ytterrr on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › merge-two-sets-in-java
Merge Two Sets in Java - GeeksforGeeks
July 11, 2025 - // Java Program to Demonstrate ... 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 ...
🌐
Techie Delight
techiedelight.com › home › java › merge two sets in java
Merge two sets in Java | Techie Delight
July 7, 2026 - The idea is to obtain a stream of elements from both sets by using the static factory method Stream.of() and accumulate all elements into a new set using a Collector, as shown below: We can avoid using a Collector by accumulating all elements using forEach() instead, as shown below: The Java Stream provides concat() that takes two streams as input and creates a lazily concatenated stream whose elements are all the elements of the first stream, followed by all the elements of the second stream.
🌐
TutorialsPoint
tutorialspoint.com › merge-two-sets-in-java
Merge two sets in Java
import java.util.stream.*; import java.util.*; import java.io.*; public class Demo{ public static <T> Set<T> set_merge(Set<T> set_1, Set<T> set_2){ Set<T> my_set = set_1.stream().collect(Collectors.toSet()); my_set.addAll(set_2); return my_set; } public static void main(String[] args){ Set<Integer> my_set_1 = new HashSet<Integer>(); my_set_1.addAll(Arrays.asList(new Integer[] { 34, 67, 89, 102 })); Set<Integer> my_set_2 = new HashSet<Integer>(); my_set_2.addAll(Arrays.asList(new Integer[] { 77, 11, 0 , -33})); System.out.println("The first set contains " + my_set_1); System.out.println("The second set contains " + my_set_2); System.out.println("The two sets are merged " + set_merge(my_set_1, my_set_2)); } }
🌐
Baeldung
baeldung.com › home › java › java collections › java – combine multiple collections
Java - Combine Multiple Collections | Baeldung
May 11, 2024 - The Guava library from Google provides several convenience methods for operating on collections and can be used with Java 6 or later. The Iterables.concat() method is one of the Guava convenient methods that is used for merging collections:
🌐
CodeSpeedy
codespeedy.com › home › merge two sets in java
Merge two sets in Java - CodeSpeedy
January 6, 2023 - So, here we take two different input sets, and then we get a new merged set which is also a sorted set which you can see. Now we will look into the code. import java.util.*; public class main { 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[] { 8,4, 3, 7, 9 })); Set<Integer> b = new HashSet<Integer>(); b.addAll(Arrays.asList(new Integer[] { 0, 5, 1, 2, 6 })); System.out.println(a); System.out.println(b); System.out.println(mergeSet(a, b)); } }
🌐
Techie Delight
techiedelight.com › home › java › merge multiple sets in java
Merge multiple sets in Java | Techie Delight
July 7, 2026 - We can use streams in Java 8 and above to merge multiple sets by obtaining a stream consisting of all elements from every set using the static factory method Stream.of() and accumulating all elements into a new set using a Collector. We can also accumulate all elements using forEach(), as shown below: Stream also has a concat() method that takes two streams as input and creates a lazily merged stream out of them.
🌐
Vultr Docs
docs.vultr.com › java › examples › calculate union of two sets
Java Program to Calculate union of two sets
December 19, 2024 - The union of setA and setB includes three different fruit names. As before, duplicates are removed, yielding the output ["apple", "banana", "cherry"]. Utilize Java 8's Stream API for a more modern approach. Merge two sets and collect the results into a new set.
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › merge-two-lists-in-java
How to Merge Two Lists in Java? | DigitalOcean
Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
🌐
Baeldung
baeldung.com › home › java › java collections › combining different types of collections in java
Combining Different Types of Collections in Java | Baeldung
April 4, 2025 - The Guava library provides us with straightforward Sets.union() method to combine Sets in Java: ... We can make use of the Map interface which itself provides us with the putAll() method which copies all of the mappings from the supplied argument ...
🌐
Coderanch
coderanch.com › t › 682461 › java › Merging-sorted-sets-set-treeset
Merging two sorted sets into one set without using treeset as the result set (Beginning Java forum at Coderanch)
July 23, 2017 - Well, if you want the result to be a LinkedHashSet, you can just do the following: If the original sets shouldn't be modified, you can do: Or: ... I moved your first line out of the code tags because it wasn't code. Look at line 68; there is no need for empty space there. That method for combining the two sets looks very complicated, particularly the while loop.
🌐
Medium
medium.com › @AlexanderObregon › merging-two-arrays-into-one-without-duplicates-in-java-6931f8f9121e
Merging Two Arrays into One Without Duplicates in Java
August 7, 2025 - HashSet is a common type of Set. It handles basic types like integers and strings by hashing them. The hash gives Java a quick way to store and look up values. When you use add, the item is turned into a hash value, and Java uses that hash to check whether the value has already been added. Here’s a simple way to merge two arrays using a set and avoid duplicates:
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › merge two arraylists in java, with or without duplicates
Merge Two ArrayLists in Java, With or Without Duplicates
January 19, 2023 - The usual way to merge two ArrayList objects is to copy the first list into a new ArrayList and call addAll() with the second, which appends every element of the second list and keeps the duplicates.
🌐
Codevisionz
codevisionz.com › home › java code example: combine hashsets
Java combine HashSets | Code Example
February 18, 2023 - import java.util.HashSet; class Cube { public static void main(String[] args) { HashSet<Integer> set1 = new HashSet<Integer>(), set2 = new HashSet<Integer>(); set1.add(1); set1.add(2); set1.add(3); set2.add(4); set2.add(5); set2.add(6); // output elements using enhanced for loop for (Integer s : set1) { System.out.println(s); } System.out.println("Combined sets:"); // Adds all of the elements in the specified collection to this collection (optional operation).
🌐
TheLinuxCode
thelinuxcode.com › home › merge two sets in java: a deep, practical guide with modern workflows
Merge Two Sets in Java: A Deep, Practical Guide with Modern Workflows – TheLinuxCode
January 7, 2026 - You’ll get concrete code for each method, plus performance notes and memory guidance from a small benchmark run on Java 21. If you only need a merged set and you’re fine with HashSet, this is the baseline I recommend for 90% of cases: ... I recommend pre-sizing the HashSet. When I merge two 1,000,000-element sets, pre-sizing reduces rehash work.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-merge-two-linkedhashset-objects-in-java
How to Merge Two LinkedHashSet Objects in Java? - GeeksforGeeks
July 23, 2025 - import java.util.LinkedHashSet; ... * To merge two LinkedHashSet objects or * append LinkedHashSet object to another, use the * addAll method */ lhSetColors1.addAll(lhSetColors2); System.out.println("Merged LinkedHashSet: " + ...