Union Too Intersection Of 2 Fix Inwards Coffee - Google Guava Example

Google Guava is an opened upwards origin library which provides lots of useful utility to Java programmer,  one of them is slow agency to uncovering intersection together with matrimony of 2 Set inward Java. You powerfulness bring used Google Guava for other functionality e.g. for overriding toString inward slow way or Immutable Collection provided yesteryear Guava library. Along alongside Apache common together with Spring, Google Guava is a library you lot definitely desire to include inward your Project. Guava provides brace of static method to operate on Set inward Java on packet com.google.common.collect.Sets. There are 2 methods called intersection() together with union() which provides intersection together with matrimony of 2 Sets inward Java. In this instance nosotros bring used HashSet every bit Set implementation merely it volition piece of job alongside whatever Set implementation e.g. TreeSet or LinkedHashSet.

How to uncovering Intersection together with Union of 2 Set inward Java

 is an opened upwards origin library which provides lots of useful utility to Java programmer Union together with Intersection of 2 Set inward Java - Google Guava ExampleHere is consummate code instance of How to calculate matrimony together with intersection of 2 Set inward Java. It use's static method Sets.intersection() together with Sets.union() to uncovering intersection together with matrimony of 2 Sets inward 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 together with 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 to a greater extent than or less 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 inward Java
        Set<String> intersection = Sets.intersection(actors, singers);
        System.out.printf("Intersection of 2 Set %s together with %s inward Java is %s %n",
                singers.toString(), actors.toString(), intersection.toString());
        System.err.println("Number of elements mutual inward 2 Set : "
                           + intersection.size());
     
        // Calculating Union of 2 Set inward Java
        Set<String> matrimony = Sets.union(actors, singers);
        System.out.printf("Union of 2 Set %s together with %s inward Java is %s %n",
                singers.toString(), actors.toString(), union.toString());
        System.out.println("total release of chemical constituent inward matrimony of 2 Set is : "
                            + union.size());
    }
     
}

Output:
Intersection of 2 Set [Medona, Shan, Amitabh Bacchan] together with [SRK, tom cruise, Amitabh Bacchan] inward Java is [Amitabh Bacchan]

Number of elements mutual inward 2 Set : 1
Union of 2 Set [Medona, Shan, Amitabh Bacchan] together with [SRK, tom cruise, Amitabh Bacchan] inward Java is [SRK, tom cruise, Amitabh Bacchan, Medona, Shan]

full release of chemical constituent inward matrimony of 2 Set is : 5


That's all on How to uncovering Union together with Intersection of 2 Set inward Java. It's really elementary if you lot are using correct opened upwards origin library e.g. Google's Guava Collection. If you lot don't similar to usage opened upwards origin library, you lot tin move write your ain method to produce the same thing, But every bit Joshua Bloch suggested inward 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 together with Spring framework API to a greater extent than together with to a greater extent than together with I did uncovering brace of gems similar calculating fourth dimension departure alongside Stopwatch inward 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 kind a Map yesteryear keys together with values inward Java? (tutorial)
How to kind an ArrayList inward ascending together with descending gild inward Java? (tutorial)
Difference betwixt ArrayList together with HashSet inward Java? (answer)
The departure betwixt TreeMap together with TreeSet inward Java? (answer)
The departure betwixt HashMap together with ConcurrentHashMap inward Java? (answer)
The departure betwixt HashMap together with LinkedHashMap inward Java? (answer)
The departure betwixt Hashtable together with HashMap inward Java? (answer)
The departure betwixt HashSet together with TreeSet inward Java? (answer)
The departure betwixt ArrayList together with LinkedList inward Java? (answer)
The departure betwixt Vector together with ArrayList inward Java? (answer)
Difference betwixt EnumMap together with HashMap inward Java

Thanks for reading this article hence far. If you lot similar this article hence delight percentage alongside your friends together with colleagues. If you lot bring whatever query or feedback hence delight drib a comment.

Belum ada Komentar untuk "Union Too Intersection Of 2 Fix Inwards Coffee - Google Guava Example"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel