HashSet Union Intersection Difference

Compute union, intersection, and difference over HashSet with addAll, retainAll, and removeAll so tag comparisons run without hand-written loops.

JavaData Integrity

Code

snippet
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

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