Union or intersection of Java Sets

We Are Going To Discuss About Union or intersection of Java Sets. So lets Start this Java Article.

Union or intersection of Java Sets

Advertisements
  1. Union or intersection of Java Sets

    The simplest one-line solution is this:
    set1.addAll(set2); // Union
    set1.retainAll(set2); // Intersection
    The above solution is destructive, meaning that contents of the original set1 my change.
    If you don't want to touch your existing sets, create a new set:

  2. Union or intersection of Java Sets

    The simplest one-line solution is this:
    set1.addAll(set2); // Union
    set1.retainAll(set2); // Intersection
    The above solution is destructive, meaning that contents of the original set1 my change.
    If you don't want to touch your existing sets, create a new set:

Solution 1

Advertisements

The simplest one-line solution is this:

set1.addAll(set2); // Union
set1.retainAll(set2); // Intersection

The above solution is destructive, meaning that contents of the original set1 my change.
If you don’t want to touch your existing sets, create a new set:

var result = new HashSet<>(set1);          // In Java 10 and above
Set<Integer> result = new HashSet<>(set1); // In Java < 10
result.addAll(set2); // Union
result.retainAll(set2); // Intersection

Original Author Mahozad Of This Content

Solution 2

Advertisements

While guava for sure is neater and pretty much standard, here’s a non destructive way to do union and intersect using only standard Java

Set s1 = Set.of(1,2,3);
Set s2 = Set.of(3,4,5);     

Set union = Stream.concat(s1.stream(),s2.stream()).collect(Collectors.toSet()); 
Set intersect = s1.stream().filter(s2::contains).collect(Collectors.toSet());

Original Author David Lilljegren Of This Content

Solution 3

Advertisements

You can achieve this using Google's Guava library. The following explanation is given below with the help of an example:

    // Set a
    Set<String> a = new HashSet<String>();
    a.add("x");
    a.add("y");
    a.add("z");

    // Set b
    Set<String> b = new HashSet<String>();
    b.add("x");
    b.add("p");
    b.add("q");

Now, Calculating Intersection of two Set in Java:

Set<String> intersection = Sets.intersection(a, b);
System.out.printf("Intersection of two Set %s and %s in Java is %s %n",
                a.toString(), b.toString(), intersection.toString());

Output: Intersection of two Set [z, y, x] and [q, p, x] in Java is [x]

Similarly, Calculating Union of two Set in Java:

Set<String> union = Sets.union(a, b);
System.out.printf("Union of two Set %s and %s in Java is %s %n",
                a.toString(), b.toString(), union.toString());

Output: Union of two Set [z, y, x] and [q, p, x] in Java is [q, p, x, z, y]

You can read more about guava library at https://google.github.io/guava/releases/18.0/api/docs/

In order to add guava library to your project, You can see https://stackoverflow.com/a/4648947/8258942

Original Author Nitin Bisht Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment