Union In Addition To Intersection Of Ii Develop Inwards Coffee - Google Guava Example

Advertisement

Masukkan script iklan 970x90px

Union In Addition To Intersection Of Ii Develop Inwards Coffee - Google Guava Example

Jumat, 12 Juni 2020

Google Guava is an opened upwards root library which provides lots of useful utility to Java programmer,  one of them is slowly agency to notice intersection as well as matrimony of 2 Set inwards Java. You powerfulness accept used Google Guava for other functionality e.g. for overriding toString inwards slowly way or Immutable Collection provided yesteryear Guava library. Along alongside Apache park as well as Spring, Google Guava is a library yous definitely desire to include inwards your Project. Guava provides yoke of static method to operate on Set inwards Java on bundle com.google.common.collect.Sets. There are 2 methods called intersection() as well as union() which provides intersection as well as matrimony of 2 Sets inwards Java. In this event nosotros accept used HashSet every bit Set implementation exactly it volition piece of occupation alongside whatever Set implementation e.g. TreeSet or LinkedHashSet.

How to notice Intersection as well as Union of 2 Set inwards Java

 is an opened upwards root library which provides lots of useful utility to Java programmer Union as well as Intersection of 2 Set inwards Java - Google Guava ExampleHere is consummate code event of How to calculate matrimony as well as intersection of 2 Set inwards Java. It use's static method Sets.intersection() as well as Sets.union() to notice intersection as well as matrimony 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 as well as 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 closed to 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 as well as %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> matrimony = Sets.union(actors, singers);
        System.out.printf("Union of 2 Set %s as well as %s inwards Java is %s %n",
                singers.toString(), actors.toString(), union.toString());
        System.out.println("total discover of chemical gene inwards matrimony of 2 Set is : "
                            + union.size());
    }
     
}

Output:
Intersection of 2 Set [Medona, Shan, Amitabh Bacchan] as well as [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] as well as [SRK, tom cruise, Amitabh Bacchan] inwards Java is [SRK, tom cruise, Amitabh Bacchan, Medona, Shan]

full discover of chemical gene inwards matrimony of 2 Set is : 5


That's all on How to notice Union as well as Intersection of 2 Set inwards Java. It's really unproblematic if yous are using correct opened upwards root library e.g. Google's Guava Collection. If yous don't similar to utilisation opened upwards root library, yous tin write your ain method to produce the same thing, But every bit Joshua Bloch suggested inwards its pop mass Effective Java, Use library method every bit much every bit possible. That's encourages me to explore to a greater extent than on Apache commons, Google Guava as well as Spring framework API to a greater extent than as well as to a greater extent than as well as I did notice yoke of gems similar calculating fourth dimension departure alongside 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 yous may like
How to form a Map yesteryear keys as well as values inwards Java? (tutorial)
How to form an ArrayList inwards ascending as well as descending fellowship inwards Java? (tutorial)
Difference betwixt ArrayList as well as HashSet inwards Java? (answer)
The departure betwixt TreeMap as well as TreeSet inwards Java? (answer)
The departure betwixt HashMap as well as ConcurrentHashMap inwards Java? (answer)
The departure betwixt HashMap as well as LinkedHashMap inwards Java? (answer)
The departure betwixt Hashtable as well as HashMap inwards Java? (answer)
The departure betwixt HashSet as well as TreeSet inwards Java? (answer)
The departure betwixt ArrayList as well as LinkedList inwards Java? (answer)
The departure betwixt Vector as well as ArrayList inwards Java? (answer)
Difference betwixt EnumMap as well as HashMap inwards Java

Thanks for reading this article therefore far. If yous similar this article therefore delight portion alongside your friends as well as colleagues. If yous accept whatever inquiry or feedback therefore delight driblet a comment.