HashSet Union Intersection Difference
Compute union, intersection, and difference over HashSet with addAll, retainAll, and removeAll so tag comparisons run without hand-written loops.
Code
import java.util.HashSet;
import java.util.Set;
public class ReleaseSets {
public static void main(String[] args) {
Set<String> deployed = new HashSet<>(Set.of("auth", "billing", "search"));
Set<String> hotfixed = new HashSet<>(Set.of("billing", "search", "email"));
// union 并集:先拷贝再 addAll,避免污染 deployed 原集合
Set<String> union = new HashSet<>(deployed);
union.addAll(hotfixed);
System.out.println("union: " + union);
// intersection 交集:retainAll 保留两边都出现的服务,需要回归验证
Set<String> intersection = new HashSet<>(deployed);
intersection.retainAll(hotfixed);
System.out.println("intersection: " + intersection);
// difference 差集:removeAll 拿到 deployed 有而 hotfix 没碰过的部分
Set<String> difference = new HashSet<>(deployed);
difference.removeAll(hotfixed);
System.out.println("difference: " + difference);
}
}Inside the snippet
Count the class ReleaseSets and the function main as the entire surface area of HashSet Union Intersection Difference.
Putting it to use
Running the file prints labeled results such as union:, intersection:, difference: — each operation’s effect is visible in the output.
Core lines
Set<String> hotfixed = new HashSet<>(Set.of("billing", "search", "email"));
Set<String> deployed = new HashSet<>(Set.of("auth", "billing", "search"));
Set<String> intersection = new HashSet<>(deployed);
Interactive Tools for This Topic
Bitwise Operations CalculatorCompute AND, OR, XOR, NOT, and shifts on 32-bit unsigned operands with binary, hex, and signed decimal views.Date Difference CalculatorCalculate the number of days, weeks, whole months, and years between two ISO dates, computed in UTC.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- What does HashSet Union Intersection Difference print when run?
- Running the file directly is enough — expect labeled output lines such as `union:`, `intersection:`, `difference:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.