Guava - Collection Utilities



Guava introduces many advanced collections based on developers' experience in application development works. Given below is a list of useful collections −

Useful Collections

Sr.No Collection name & Description
1 Multiset

An extension to Set interface to allow duplicate elements.

2 Multimap

An extension to Map interface so that its keys can be mapped to multiple values at a time.

3 BiMap

An extension to Map interface to support inverse operations.

4 Table

Table represents a special map where two keys can be specified in combined fashion to refer to a single value.

Example - Counting elements of a Multiset

GuavaTester.java

package com.tutorialspoint;

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());
   }
}

Output

Run the GuavaTester and verify the output.

Occurence of 'b' : 5
Total Size : 10

Example - Printing maps of a Multimap

GuavaTester.java

package com.tutorialspoint;

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());
	  
      List<String> upperList = (List<String>)multimap.get("upper");
      System.out.println("Initial upper case list");
      System.out.println(upperList.toString());
	  
   }

   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;
   }
}

Output

Run the GuavaTester and verify the output −

Initial lower case list
[a, b, c, d, e]
Initial upper case list
[A, B, C, D]

Example - Getting Key based on Value from a BiMap

GuavaTester.java

package com.tutorialspoint;

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(Integer.valueOf(101), "Mahesh");
      empIDNameMap.put(Integer.valueOf(102), "Sohan");
      empIDNameMap.put(Integer.valueOf(103), "Ramesh");
      
      //Emp Id of Employee "Mahesh"
      System.out.println(empIDNameMap.inverse().get("Mahesh"));
   }	
}

Output

Run the GuavaTester and verify the output −

101
Advertisements