Disclosure: This text might include affiliate hyperlinks. While you buy, we might earn a small fee.
Guava is an open-source library that gives a lot of helpful utility to Java
programmers, certainly one of them is a simple approach to discover the intersection and union of two Units in Java. You might need used Google
Guava for different performance e.g. for overriding
toString in a simple approach or Immutable Assortment supplied by Guava library.
Together with Apache commons and Spring, Google Guava is a library you positively need
to incorporate in your Challenge. Guava offers a few static
strategies to function on Set
in Java on package deal com.google.frequent.accumulate.Units.
two strategies referred to as intersection() and union() which
offers intersection and union of two
Units in Java. On this instance, we’ve used HashSet
as Set implementation however it would work with any Set implementation e.g. TreeSet
or LinkedHashSet.
discover Intersection and Union of two
Set in Java
to calculate union and intersection of two Units in Java. It use’s static
technique Units.intersection() and Units.union() to seek out
intersection and union of two Units in Java.
import com.google.frequent.accumulate.Units;
import java.util.HashSet;
import java.util.Set;
/**
*
* Java program to reveal calculate Intersection and Union of
two
* Set utilizing Google’s Guava library instance.
*
* @creator http://javarevisited.blogspot.com.au
*/
public class
SetIntersectionExample {
public static void
major(String args[]) {
// Set which shops
some Singer
Set<String> singers = new
HashSet<String>();
singers.add(“Amitabh
Bacchan”);
singers.add(“Shan”);
singers.add(“Medona”);
// One other Set which
shops 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 = Units.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(“Variety of parts frequent in two Set : “
+ intersection.measurement());
// Calculating Union
of two Set in Java
Set<String> union = Units.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(“complete variety of ingredient in union of two Set is :
“
}
}
Output:
Intersection of two Set [Medona,
Shan, Amitabh Bacchan] and [SRK, tom cruise, Amitabh Bacchan] in Java is [Amitabh
Bacchan]
Quantity of parts frequent in two Set : 1
Union of two Set [Medona, Shan,
Amitabh Bacchan] and [SRK, tom cruise, Amitabh Bacchan]
in Java is [SRK, tom cruise, Amitabh
Bacchan, Medona, Shan]
The whole variety of parts within the union of two Set is : 5
It is quite simple if you’re utilizing proper open-source library e.g. Google’s Guava
Assortment. In the event you don’t love to make use of the open-source library, you may write your
personal technique to do the identical factor, However as Joshua Bloch prompt in his standard
guide Efficient Java, Use the library technique as a lot as potential.
Different Java Assortment tutorials you might like
- type a Map by keys and values in Java? (tutorial)
- type an ArrayList in ascending and descending order in Java? (tutorial)
- Distinction between ArrayList and HashSet in Java? (reply)
- The distinction between TreeMap and TreeSet in Java? (reply)
- The distinction between HashMap and ConcurrentHashMap in Java? (reply)
- The distinction between HashMap and LinkedHashMap in Java? (reply)
- The distinction between Hashtable and HashMap in Java? (reply)
- The distinction between HashSet and TreeSet in Java? (reply)
- The distinction between ArrayList and LinkedList in Java? (reply)
- The distinction between Vector and ArrayList in Java? (reply)
- Distinction between EnumMap and HashMap in Java
Thanks for studying this text up to now. In the event you like this text then please share it with your pals and colleagues. In case you have any questions or suggestions then please drop a remark.