Friday 21 November 2014

Multimap using Google Guava API

Multimap concept provided by Google Guava API can store multiple 'values' for the same 'key'.

If you want to have multiple values for a same key using java collection api(i.e Map) it is not possible. Since every time you add a value(i.e. using 'put' method call) to the same/single key, the value for that key is replaced with the new value.

Below program-1 demonstrates trying to store multiple values for the same key without using the Multimap concept.

Program-1 :
Without using Multimap concept
import java.util.HashMap;
import java.util.Map;

public class MapExample1 {
    public static void main(String[] args) {
       
        Map<String,String> myLanguages = new HashMap<String, String>();
       
        myLanguages.put("programmingLanguage","C++");
        myLanguages.put("programmingLanguage","C#");
        myLanguages.put("programmingLanguage","Java");
       
        myLanguages.put("scriptingLanguage","VBScript");
        myLanguages.put("scriptingLanguage","JavaScript");
       
        //Prints the values for the key.
        System.out.println("size: "+myLanguages.size());
       
        System.out.println("\n---- Print the values for key 'programmingLanguage' ----");
        System.out.println(myLanguages.get("programmingLanguage"));
       
        System.out.println("\n---- Print the values for key 'scriptingLanguage' ----");
        System.out.println(myLanguages.get("scriptingLanguage"));
    }
}


Output:
Program-1 shows the output having the key with single value. Every time new value is added to the key, old value is replaced with the new value for the key.

Now lets modify the Program-1 (i.e above program) with Multimap concept provided by Google Guava API

Step 1: 
Download the "guava-<<version>>.jar" from the URL: https://code.google.com/p/guava-libraries/

Step 2:
- Copy the  "guava-<<version>>.jar" in to the 'lib' folder of your project.
- Set the java build path for "guava-<<version>>.jar".
 
 
Program-2 :
Using Multimap concept
Modifying the the above program-1 using MultiMap(i.e. Google Guava API).

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
 

public class MultiMapExample {
    public static void main(String[] args) {
      
        Multimap<String, String> myLanguages = ArrayListMultimap.create();
      
        myLanguages.put("programmingLanguage", "C++");
        myLanguages.put("programmingLanguage", "C#");
        myLanguages.put("programmingLanguage", "Java");
      
        myLanguages.put("scriptingLanguage", "VBScript");
        myLanguages.put("scriptingLanguage", "JavaScript");
      
        int size = myLanguages.size();
        System.out.println("myLanguages size : "+size);
        System.out.println("");
              
        //Prints the values for the key.
        System.out.println("Print the values for key 'programmingLanguage'.");
        System.out.println(myLanguages.get("programmingLanguage"));
        System.out.println("");

        System.out.println("Print the values for key 'scriptingLanguage'.");
        System.out.println(myLanguages.get("scriptingLanguage"));
    }
}


Output:
Output for the progam-2 show's multiple values for the same 'key'. Every time new 'value' is added for the same 'key', old 'values' for the 'key' remains and new 'value' is added to the 'key'.

Program-3 :
Above Program-2 uses Multimap for adding the multiple values for the same/single key.  
 
Current Program-3 demonstrates adding the multiple values for the same/single key without using the Multimap API. 
This can be achieved by using the below data structure.
Map<String,List<String>> myLanguages = new HashMap<String,List<String>>();

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapExample2 {
    public static void main(String[] args) {
      
        Map<String,List<String>> myLanguages = new HashMap<String,List<String>>();
      
        myLanguages.put("programmingLanguage", Arrays.asList("C++","C#","Java"));
        myLanguages.put("scriptingLanguage", Arrays.asList("VBScript","JavaScript"));
      
        System.out.println("size: "+myLanguages.size());
      
        System.out.println("\n---- Print the values for key 'programmingLanguage' ----");
        System.out.println(myLanguages.get("programmingLanguage"));
      
        System.out.println("\n---- Print the values for key 'scriptingLanguage' ----");
        System.out.println(myLanguages.get("scriptingLanguage"));
        System.out.println("");
    }
}

Output:
Program-3 stores multiple values in the list, once all the values are added to the list finally list added to the 'key'.

Program-4 :
Using Multimap - Removing the values for the key
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class MultiMapRemove {
    public static void main(String[] args) {
       
        Multimap<String, String> myLanguages = ArrayListMultimap.create();
       
        myLanguages.put("programmingLanguage", "C");
        myLanguages.put("programmingLanguage", "C++");
        myLanguages.put("programmingLanguage", "C#");
        myLanguages.put("programmingLanguage", "Java");
       
        myLanguages.put("scriptingLanguage", "VBScript");
        myLanguages.put("scriptingLanguage", "JavaScript");
       
        System.out.println("myLanguages size : "+myLanguages.size());
        System.out.println("");
               
        //Prints the values for the key.
        System.out.println("---- Print the values for key 'programmingLanguage' ----");
        System.out.println(myLanguages.get("programmingLanguage"));
        System.out.println("");

        System.out.println("---- Print the values for key 'scriptingLanguage' ----");
        System.out.println(myLanguages.get("scriptingLanguage"));
        System.out.println("");
       
        //remove single value
        System.out.println("---- remove single value for key 'programmingLanguage'----");
        myLanguages.remove("programmingLanguage", "C");
        System.out.println("myLanguages size : "+myLanguages.size());
        System.out.println(myLanguages.get("programmingLanguage"));
        System.out.println("");

        //remove all values
        System.out.println("---- remove all values key 'programmingLanguage'----");
        myLanguages.removeAll("programmingLanguage");
        System.out.println("myLanguages size : "+myLanguages.size());
        System.out.println(myLanguages.get("programmingLanguage"));
        System.out.println("");
    }
}


Output:

Program-5 :
Using Multimap - Replacing the values for the key
Below program demonstrates replacing the values for the key.

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class MultiMapReplace {
    public static void main(String[] args) {
      
        Multimap<String, String> myLanguages = ArrayListMultimap.create();
      
        myLanguages.put("programmingLanguage", "C");
        myLanguages.put("programmingLanguage", "C#");
        myLanguages.put("programmingLanguage", "Java");
      
        myLanguages.put("scriptingLanguage", "JavaScript");
      
        //Prints the values for the key.
        System.out.println("size: "+myLanguages.size());
        System.out.println("---- Print the values for key 'programmingLanguage' ----");
        System.out.println(myLanguages.get("programmingLanguage"));
        System.out.println("---- Print the values for key 'scriptingLanguage' ----");
        System.out.println(myLanguages.get("scriptingLanguage"));
        System.out.println("");
      
        //Replace the values for the key.
        Iterable<String> iterable = myLanguages.get("scriptingLanguage");
        myLanguages.replaceValues("programmingLanguage", iterable);
        System.out.println("size: "+myLanguages.size());

        System.out.println("---- Print the values for key 'programmingLanguage' ----");
        System.out.println(myLanguages.get("programmingLanguage"));
        System.out.println("---- Print the values for key 'scriptingLanguage' ----");
        System.out.println(myLanguages.get("scriptingLanguage"));
    }
}
 

Output:
Note:  Above program-5 replaces all the values for the key 'programmingLanguage' with all the values of the key 'scriptingLanguage'.

Environment used for demonstrating the above example programs:
Google Guava - guava-18.0.jar
Java - 1.6

No comments:

Post a Comment