Guava – Multimap

Not to be confused with the fruit, Guava is an open source, Java based library developed by Google. It provides utility methods for collections, caching, primitives support, concurrency, common annotations, string processing, I/O, and validations.

I have been experimenting and using the Google Guava library for a few months. Most of features are quite nice and useful (e.g., Multimaps). In this post I show how easy it is interact with multimaps.

One of the main features is that a key may have different values. That is not possible with standard Java maps.

Following is a screen capture of the console from the Eclipse IDE:

main <<< size: 10
main <<< mmap: {decimal=[0, 1, 2, 3], hexadecimal=[A, B, C, D, E, G]}

main <<< decimals: [0, 1, 2, 3]
main <<< decimals: [0, 1, 2, 3, F]

main <<< hexadecimals: [A, B, C, D, E, G]
main <<< hexadecimals: [A, B, C, D, E]

main <<< decimals: [A, B, C, D, E, F]

main <<< key: decimal -> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
main <<< key: hexadecimal -> [A, B, C, D, E, F]

main <<< key: decimal
main <<< key: hexadecimal

main <<<   size: 16
main <<<   mmap: {decimal=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], hexadecimal=[A, B, C, D, E, F]}
main <<< values: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F]

For starters a multimaps is created and incorrectly (0xG is not a hexadecimal digit) populated with decimals (some are missing) and hexadecimal digits.

Two lists are created and manipulated; one for the decimals and the other for the hexadecimals.

Towards the end of the code we see that the lists have modified the contents of the multimaps.

The associated Java 8 code follows:


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;

/**
 * Experiment with the Guava Multimap class.
 */
public class Solution {

	/**
	 * Test code.
	 */
	public static void main(String[] args) {

		// **** instantiate a multimap ****
		
		Multimap<String, String> mmap = ArrayListMultimap.create();
		
		// **** populate the multimap ****
		
		mmap.put("decimal", "0");
		mmap.put("decimal", "1");
		mmap.put("decimal", "2");
		mmap.put("decimal", "3");		// missing 4 - 9
		
		mmap.put("hexadecimal", "A");
		mmap.put("hexadecimal", "B");
		mmap.put("hexadecimal", "C");
		mmap.put("hexadecimal", "D");
		mmap.put("hexadecimal", "E");
		mmap.put("hexadecimal", "G");	// incorrect

		// **** display the size and contents of the multimap ****
		
		System.out.println("main <<< size: " + mmap.size());
		System.out.println("main <<< mmap: " + mmap.toString() + "\n");
		
		// **** get the list of decimals ****
		
		List<String> decimals = (List<String>)mmap.get("decimal");
		System.out.println("main <<< decimals: " + decimals.toString());
		
		// **** add a new decimal (wrong choice) ****
		
		decimals.add("F");
		
		// **** display the list of decimals ****
		
		System.out.println("main <<< decimals: " + decimals.toString() + "\n");
		
		// **** remove 'F' (does not belong in this list) ****
		
		decimals.remove("F");
		
		// **** add the rest of decimals ****
		
		decimals.add("4");
		decimals.add("5");
		decimals.add("6");
		decimals.add("7");
		decimals.add("8");
		decimals.add("9");
			
		// **** get the list of hexadecimals ****
		
		List<String> hexadecimals = (List<String>)mmap.get("hexadecimal");
		System.out.println("main <<< hexadecimals: " + hexadecimals.toString());
		
		// **** modify the hexadecimal list ("G" does not belong) ****
		
		hexadecimals.remove("G");
		
		// **** display the modified hexadecimal list ****
		
		System.out.println("main <<< hexadecimals: " + hexadecimals.toString() + "\n");
		
		// **** ****
		
		hexadecimals.add("F");
		
		// **** display the modified hexadecimal list ****
		
		System.out.println("main <<< decimals: " + hexadecimals.toString() + "\n");
		
		// **** ****
		
		Map<String, Collection<String>> map = mmap.asMap();
		
		// **** display the map ****
		
		for (Map.Entry<String, Collection<String>> entry : map.entrySet()) {
			String key = entry.getKey();
			Collection<String> value = mmap.get(key);
			System.out.println("main <<< key: " + key + " -> " + value);
		}
		System.out.println();
		
		// **** keys in the multimap ****
		
		Set<String> keys = mmap.keySet();
		for (String key : keys) {
			System.out.println("main <<< key: " + key);
		}
		System.out.println();
		
		// **** get the collection of values in the multimap ****
		
		Collection<String> values = mmap.values();
		System.out.println("main <<<   size: " + mmap.size());
		System.out.println("main <<<   mmap: " + mmap.toString());
		System.out.println("main <<< values: " + values.toString());
	}

}

Note that you need to include the Guava libraries.

For additional information and examples on Guava I found the tutorials at tutorialspoint quite useful. If you are interested in the source and associated documentation for Guava, take a look at this GitHub repository. The following DEVOXX session from 2012 on YouTube might be somewhat outdated but it provides an overview (55:06 minutes) on Guava.

I have noticed that on a few occasions when I insert code into the post, some source lines do not show. If this is the case please let me know. I will try to modify the code on the post to make all lines appear. If you wish, I can also send you a complete copy of the code.

If you have comments or questions regarding this or any other post please leave your comments. I will take action as soon as possible.

Enjoy;

John

Follow me on Twitter:  @john_canessa

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.