Guava - COLLECTIONS UTILITIESGuava introduces many advanced collections based on developers' experience in application development works. Given below is a list of useful collections:
Multiset interface extends ‘Set’ to have duplicate elements, and provides various utility methods to deal with the occurrences of such elements in a set. Interface Declaration Following is the declaration for com.google.common.collect.Multiset<E> interface:
@GwtCompatible
public interface Multiset<E> extends Collection<E> Interface Methods
This interface inherits methods from the following interface:
Create the following java program using any editor of your choice in say C:/> Guava. GuavaTester.java
import java.util.Iterator;
import java.util.Set; import com.google.common.collect.HashMultiset; import com.google.common.collect.Multiset; public class GuavaTester { public static void main(String args[]){ //create a multiset collection Multiset<String> multiset = HashMultiset.create(); multiset.add("a"); multiset.add("b"); multiset.add("c"); multiset.add("d"); multiset.add("a"); multiset.add("b"); multiset.add("c"); multiset.add("b"); multiset.add("b"); multiset.add("b"); //print the occurrence of an element System.out.println("Occurrence of 'b' : "+multiset.count("b")); //print the total size of the multiset System.out.println("Total Size : "+multiset.size()); //get the distinct elements of the multiset as set Set<String> set = multiset.elementSet(); //display the elements of the set System.out.println("Set ["); for (String s : set) { System.out.println(s); } System.out.println("]"); //display all the elements of the multiset using iterator Iterator<String> iterator = multiset.iterator(); System.out.println("MultiSet ["); while(iterator.hasNext()){ System.out.println(iterator.next()); } System.out.println("]"); //display the distinct elements of the multiset with their occurrence count System.out.println("MultiSet ["); for (Multiset.Entry<String> entry : multiset.entrySet()) { System.out.println("Element: "+entry.getElement() +", Occurrence(s): " + entry.getCount()); } System.out.println("]"); //remove extra occurrences multiset.remove("b",2); //print the occurrence of an element System.out.println("Occurence of 'b' : "+multiset.count("b")); } } Verify the Result Compile the class using javac compiler as follows:
C:\Guava>javac GuavaTester.java
Now run the GuavaTester to see the result.
C:\Guava>java GuavaTester
See the result.
Occurence of 'b' : 5
Total Size : 10 Set [ d b c a ] MultiSet [ d b b b b b c c a a ] MultiSet [ Element: d, Occurence(s): 1 Element: b, Occurence(s): 5 Element: c, Occurence(s): 2 Element: a, Occurence(s): 2 ] Occurence of 'b' : 3 Multimap Interface Multimap interface extends Map so that its keys can be mapped to multiple values at a time. Interface Interface Interface Interface Interface Interface Interface Interface Interface Interface Declaration Following is the declaration for com.google.common.collect.Multimap<K,V> interface:
@GwtCompatible
public interface Multimap<K,V> Interface Methods
Create the following java program using any editor of your choice in say C:/> Guava. GuavaTester.java
import java.util.Collection;
import java.util.List; import java.util.Map; import java.util.Set; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; public class GuavaTester { public static void main(String args[]){ GuavaTester tester = new GuavaTester(); Multimap<String,String> multimap = tester.getMultimap(); List<String> lowerList = (List<String>)multimap.get("lower"); System.out.println("Initial lower case list"); System.out.println(lowerList.toString()); lowerList.add("f"); System.out.println("Modified lower case list"); System.out.println(lowerList.toString()); List<String> upperList = (List<String>)multimap.get("upper"); System.out.println("Initial upper case list"); System.out.println(upperList.toString()); upperList.remove("D"); System.out.println("Modified upper case list"); System.out.println(upperList.toString()); Map<String, Collection<String>> map = multimap.asMap(); System.out.println("Multimap as a map"); for (Map.Entry<String, Collection<String>> entry : map.entrySet()) { String key = entry.getKey(); Collection<String> value = multimap.get("lower"); System.out.println(key + ":" + value); } System.out.println("Keys of Multimap"); Set<String> keys = multimap.keySet(); for(String key:keys){ System.out.println(key); } System.out.println("Values of Multimap"); Collection<String> values = multimap.values(); System.out.println(values); } private Multimap<String,String> getMultimap(){ //Map<String, List<String>> // lower -> a, b, c, d, e // upper -> A, B, C, D Multimap<String,String> multimap = ArrayListMultimap.create(); multimap.put("lower", "a"); multimap.put("lower", "b"); multimap.put("lower", "c"); multimap.put("lower", "d"); multimap.put("lower", "e"); multimap.put("upper", "A"); multimap.put("upper", "B"); multimap.put("upper", "C"); multimap.put("upper", "D"); return multimap; } } Verify the Result Compile the class using javac compiler as follows:
C:\Guava>javac GuavaTester.java
Now run the GuavaTester to see the result.
C:\Guava>java GuavaTester
See the result.
Initial lower case list
[a, b, c, d, e] Modified lower case list [a, b, c, d, e, f] Initial upper case list [A, B, C, D] Modified upper case list [A, B, C] Multimap as a map upper:[a, b, c, d, e, f] lower:[a, b, c, d, e, f] Keys of Multimap upper lower Values of Multimap [A, B, C, a, b, c, d, e, f] Bi Map Interface A BiMap is a special kind of map which maintains an inverse view of the map while ensuring that no duplicate values are present in the map and a value can be used safely to get the key back. Interface Interface Interface Interface Interface Interface Interface Interface Interface Interface Declaration Following is the declaration for com.google.common.collect.Bimap<K,V> interface:
@GwtCompatible
public interface BiMap<K,V> extends Map<K,V>
This class inherits methods from the following interface:
Create the following java program using any editor of your choice in say C:/> Guava. GuavaTester.java
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap; public class GuavaTester { public static void main(String args[]){ BiMap<Integer, String> empIDNameMap = HashBiMap.create(); empIDNameMap.put(new Integer(101), "Mahesh"); empIDNameMap.put(new Integer(102), "Sohan"); empIDNameMap.put(new Integer(103), "Ramesh"); //Emp Id of Employee "Mahesh" System.out.println(empIDNameMap.inverse().get("Mahesh")); } } Verify the Result Compile the class using javac compiler as follows:
C:\Guava>javac GuavaTester.java
Now run the GuavaTester to see the result.
C:\Guava>java GuavaTester
See the result.
101
Table Interface Table represents a special map where two keys can be specified in combined fashion to refer to a single value. It is similar to creating a map of maps. Interface Declaration Following is the declaration for com.google.common.collect.Table<R,C,V> interface:
@GwtCompatible
public interface Table<R,C,V> Interface Methods
Create the following java program using any editor of your choice in say C:/> Guava. GuavaTester.java
import java.util.Map;
import java.util.Set; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; public class GuavaTester { public static void main(String args[]){ //Table<R,C,V> == Map<R,Map<C,V>> /* * Company: IBM, Microsoft, TCS * IBM -> {101:Mahesh, 102:Ramesh, 103:Suresh} * Microsoft -> {101:Sohan, 102:Mohan, 103:Rohan } * TCS -> {101:Ram, 102: Shyam, 103: Sunil } * * */ //create a table Table<String, String, String> employeeTable = HashBasedTable.create(); //initialize the table with employee details employeeTable.put("IBM", "101","Mahesh"); employeeTable.put("IBM", "102","Ramesh"); employeeTable.put("IBM", "103","Suresh"); employeeTable.put("Microsoft", "111","Sohan"); employeeTable.put("Microsoft", "112","Mohan"); employeeTable.put("Microsoft", "113","Rohan"); employeeTable.put("TCS", "121","Ram"); employeeTable.put("TCS", "122","Shyam"); employeeTable.put("TCS", "123","Sunil"); //get Map corresponding to IBM Map<String,String> ibmEmployees = employeeTable.row("IBM"); System.out.println("List of IBM Employees"); for(Map.Entry<String, String> entry : ibmEmployees.entrySet()){ System.out.println("Emp Id: " + entry.getKey() + ", Name: " + entry.getValue()); } //get all the unique keys of the table Set<String> employers = employeeTable.rowKeySet(); System.out.print("Employers: "); for(String employer: employers){ System.out.print(employer + " "); } System.out.println(); //get a Map corresponding to 102 Map<String,String> EmployerMap = employeeTable.column("102"); for(Map.Entry<String, String> entry : EmployerMap.entrySet()){ System.out.println("Employer: " + entry.getKey() + ", Name: " + entry.getValue()); } } } Verify the Result Compile the class using javac compiler as follows:
C:\Guava>javac GuavaTester.java
Now run the GuavaTester to see the result.
C:\Guava>java GuavaTester
See the result.
List of IBM Employees
Emp Id: 102, Name: Ramesh Emp Id: 101, Name: Mahesh Emp Id: 103, Name: Suresh Employers: IBM TCS Microsoft Employer: IBM, Name: Ramesh |