Union As Well As Intersection Of 2 Develop Inwards Coffee - Google Guava Example
Saturday, June 23, 2018
Add Comment
Google Guava is an opened upwards origin library which provides lots of useful utility to Java programmer, one of them is slow agency to honour intersection in addition to wedlock of 2 Set inwards Java. You powerfulness bring used Google Guava for other functionality e.g. for overriding toString inwards slow way or Immutable Collection provided yesteryear Guava library. Along amongst Apache green in addition to Spring, Google Guava is a library you lot definitely desire to include inwards your Project. Guava provides yoke of static method to operate on Set inwards Java on packet com.google.common.collect.Sets. There are 2 methods called intersection() in addition to union() which provides intersection in addition to wedlock of 2 Sets inwards Java. In this instance nosotros bring used HashSet equally Set implementation only it volition piece of work amongst whatever Set implementation e.g. TreeSet or LinkedHashSet.
How to honour Intersection in addition to Union of 2 Set inwards Java
Here is consummate code instance of How to calculate wedlock in addition to intersection of 2 Set inwards Java. It use's static method Sets.intersection() in addition to Sets.union() to honour intersection in addition to wedlock of 2 Sets inwards Java.
package test;
import com.google.common.collect.Sets;
import java.util.HashSet;
import java.util.Set;
/**
*
* Java programme to demonstrate How to calculate Intersection in addition to 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 roughly 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 2 Set inwards Java
Set<String> intersection = Sets.intersection(actors, singers);
System.out.printf("Intersection of 2 Set %s in addition to %s inwards Java is %s %n",
singers.toString(), actors.toString(), intersection.toString());
System.err.println("Number of elements mutual inwards 2 Set : "
import com.google.common.collect.Sets;
import java.util.HashSet;
import java.util.Set;
/**
*
* Java programme to demonstrate How to calculate Intersection in addition to 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 roughly 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 2 Set inwards Java
Set<String> intersection = Sets.intersection(actors, singers);
System.out.printf("Intersection of 2 Set %s in addition to %s inwards Java is %s %n",
singers.toString(), actors.toString(), intersection.toString());
System.err.println("Number of elements mutual inwards 2 Set : "
+ intersection.size());
// Calculating Union of 2 Set inwards Java
Set<String> wedlock = Sets.union(actors, singers);
System.out.printf("Union of 2 Set %s in addition to %s inwards Java is %s %n",
singers.toString(), actors.toString(), union.toString());
System.out.println("total reveal of chemical component inwards wedlock of 2 Set is : "
// Calculating Union of 2 Set inwards Java
Set<String> wedlock = Sets.union(actors, singers);
System.out.printf("Union of 2 Set %s in addition to %s inwards Java is %s %n",
singers.toString(), actors.toString(), union.toString());
System.out.println("total reveal of chemical component inwards wedlock of 2 Set is : "
+ union.size());
}
}
Output:
Intersection of 2 Set [Medona, Shan, Amitabh Bacchan] in addition to [SRK, tom cruise, Amitabh Bacchan] inwards Java is [Amitabh Bacchan]
Number of elements mutual inwards 2 Set : 1
Union of 2 Set [Medona, Shan, Amitabh Bacchan] in addition to [SRK, tom cruise, Amitabh Bacchan] inwards Java is [SRK, tom cruise, Amitabh Bacchan, Medona, Shan]
full reveal of chemical component inwards wedlock of 2 Set is : 5
}
}
Output:
Intersection of 2 Set [Medona, Shan, Amitabh Bacchan] in addition to [SRK, tom cruise, Amitabh Bacchan] inwards Java is [Amitabh Bacchan]
Number of elements mutual inwards 2 Set : 1
Union of 2 Set [Medona, Shan, Amitabh Bacchan] in addition to [SRK, tom cruise, Amitabh Bacchan] inwards Java is [SRK, tom cruise, Amitabh Bacchan, Medona, Shan]
full reveal of chemical component inwards wedlock of 2 Set is : 5
That's all on How to honour Union in addition to Intersection of 2 Set inwards Java. It's really uncomplicated if you lot are using correct opened upwards origin library e.g. Google's Guava Collection. If you lot don't similar to occupation opened upwards origin library, you lot tin write your ain method to create the same thing, But equally Joshua Bloch suggested inwards its pop majority Effective Java, Use library method equally much equally possible. That's encourages me to explore to a greater extent than on Apache commons, Google Guava in addition to Spring framework API to a greater extent than in addition to to a greater extent than in addition to I did honour yoke of gems similar calculating fourth dimension divergence amongst Stopwatch inwards Spring framework which helps a lot.
Further Learning
Java Fundamentals: The Java Language yesteryear Jim Wilson
Java Fundamentals: Collections
Head First Java
Other Java Collection tutorials you lot may like
How to variety a Map yesteryear keys in addition to values inwards Java? (tutorial)
How to variety an ArrayList inwards ascending in addition to descending companionship inwards Java? (tutorial)
Difference betwixt ArrayList in addition to HashSet inwards Java? (answer)
The divergence betwixt TreeMap in addition to TreeSet inwards Java? (answer)
The divergence betwixt HashMap in addition to ConcurrentHashMap inwards Java? (answer)
The divergence betwixt HashMap in addition to LinkedHashMap inwards Java? (answer)
The divergence betwixt Hashtable in addition to HashMap inwards Java? (answer)
The divergence betwixt HashSet in addition to TreeSet inwards Java? (answer)
The divergence betwixt ArrayList in addition to LinkedList inwards Java? (answer)
The divergence betwixt Vector in addition to ArrayList inwards Java? (answer)
Difference betwixt EnumMap in addition to HashMap inwards Java
The divergence betwixt TreeMap in addition to TreeSet inwards Java? (answer)
The divergence betwixt HashMap in addition to ConcurrentHashMap inwards Java? (answer)
The divergence betwixt HashMap in addition to LinkedHashMap inwards Java? (answer)
The divergence betwixt Hashtable in addition to HashMap inwards Java? (answer)
The divergence betwixt HashSet in addition to TreeSet inwards Java? (answer)
The divergence betwixt ArrayList in addition to LinkedList inwards Java? (answer)
The divergence betwixt Vector in addition to ArrayList inwards Java? (answer)
Difference betwixt EnumMap in addition to HashMap inwards Java
Thanks for reading this article hence far. If you lot similar this article in addition to hence delight portion amongst your friends in addition to colleagues. If you lot bring whatever enquiry or feedback in addition to hence delight drib a comment.
0 Response to "Union As Well As Intersection Of 2 Develop Inwards Coffee - Google Guava Example"
Post a Comment