**Google Guava**is an open source library which provides lots of useful utility to Java programmer, one of them is easy way to find

**intersection and union of two Set in Java**. You might have used Google Guava for other functionality e.g. for overriding toString in easy way or Immutable Collection provided by Guava library. Along with Apache commons and Spring, Google Guava is a library you definitely want to include in your Project. Guava provides couple of static method to operate on Set in Java on package com.google.common.collect.Sets. There are two methods called intersection() and union() which provides

*intersection and union of two Sets in Java*. In this example we have used HashSet as Set implementation but it will work with any Set implementation e.g. TreeSet or LinkedHashSet.

##
__How to find Intersection and Union of two
Set in Java__

Here is complete code example of

*How to calculate union and intersection of two Set in Java*. It use's static method Sets.intersection() and Sets.union() to find intersection and union of two Sets in Java.**package**test;

**import**com.google.common.collect.Sets;

**import**java.util.HashSet;

**import**java.util.Set;

/**

*

*

**Java program to demonstrate How to calculate Intersection and Union**of two

* Set using Google's Guava library example.

*

* @author http://javarevisited.blogspot.com.au

*/

**public**

**class**SetIntersectionExample {

**public**

**static**

**void**main(

**String**args[]) {

*// Set which stores some Singer*

**Set**<

**String**> singers =

**new**

**HashSet**<

**String**>();

singers.add("Amitabh Bacchan");

singers.add("Shan");

singers.add("Medona");

*// Another Set which stores Actors*

**Set**<

**String**> actors =

**new**

**HashSet**<

**String**>();

actors.add("Amitabh Bacchan");

actors.add("tom cruise");

actors.add("SRK");

*// Calculating Intersection of two Set in Java*

**Set**<

**String**> intersection = Sets.intersection(actors, singers);

**System**.out.printf("Intersection of two Set %s and %s in Java is %s %n",

singers.toString(), actors.toString(), intersection.toString());

**System**.err.println("Number of elements common in two Set : "

+ intersection.size());

singers.toString(), actors.toString(), union.toString());

*// Calculating Union of two Set in Java***Set**<**String**> union = Sets.union(actors, singers);**System**.out.printf("Union of two Set %s and %s in Java is %s %n",singers.toString(), actors.toString(), union.toString());

**System**.out.println("total number of element in union of two Set is : "
+ union.size());

}

}

Intersection of two

Union of two

total number of element in union of two

}

}

**Output:**Intersection of two

**Set**[Medona, Shan, Amitabh Bacchan] and [SRK, tom cruise, Amitabh Bacchan] in Java is [Amitabh Bacchan]**Number**of elements common in two**Set**: 1Union of two

**Set**[Medona, Shan, Amitabh Bacchan] and [SRK, tom cruise, Amitabh Bacchan] in Java is [SRK, tom cruise, Amitabh Bacchan, Medona, Shan]total number of element in union of two

**Set**is : 5
That's all on

**How to find Union and Intersection of two Set in Java**. It's very simple if you are using right open source library e.g. Google's Guava Collection. If you don't like to use open source library, you can write your own method to do the same thing, But as Joshua Bloch suggested in its popular book Effective Java, Use library method as much as possible. That's encourages me to explore more on Apache commons, Google Guava and Spring framework API more and more and I did find couple of gems like calculating time difference with Stopwatch in Spring framework which helps a lot.**Related topics:**

## 2 comments :

We can use addAll() and retainAll() methods of List and Set for this.

allAll() and retainAll() methods also modify the collection. These fucntions do not

## Post a Comment