Skip to main content

Guava: Multimap, Bimap

Multimap is a collection similar to a Map, but which may associate multiple values with a single key. So if you put the two value with the same key then multimap will map both value with the same key.

Multimap has some very useful extension like ListMultimap, SetMultimap

ArrayListMultimap implements the ListMultimap interface and uses an ArrayList to store the values for a given key.

And  HashMultimap implements the SetMultimap interface and it will not store the duplicate key-value pairs, same as Set behave.

Here is the ArrayListMultimap example.

Multimap<String, String> map1 = ArrayListMultimap.create();
map1.put("A", "Test1");
map1.put("B", "Test2");
map1.put("B", "Test3");
map1.put("A", "Test4");
map1.put("C", "Test5");
map1.put("X", "Test6");
Multimap<String, String> map2 = ArrayListMultimap.create();
map2.put("A", "Test1");
map2.put("A", "Test2");
map2.put("B", "Test3");
map2.put("B", "Test4");
map2.put("C", "Test5");
map2.put("Y", "Test6"); 

This ArrayListMultimap will arrange the value in the map like it shown in this image

some useful function provided by the Multimap.
Map<K,Collection<V>> asMap()
boolean containsEntry(Object key, Object value)
boolean containsKey(Object key)  
boolean containsValue(Object value)
static <K,V> ArrayListMultimap<K,V>   create()
Set<K> keySet()
Collection<V> values()

Here are some example code.

["A", "B", "C", "X"]

["Test1", "Test4", "Test2", "Test3", "Test5", "Test4"]

["A"="Test1", "A"="Test4", "B"="Test2", "B"="Test3", "C"="Test5", "X"="Test6"]

Working with collection some time also required the comparison and for the same purpose guava as a class MapDifference and MapDifference.ValueDifference.

How to use it !!!

MapDifference<String, ?> differenceMap = Maps.difference(map1.asMap(), map2.asMap());


{"A"=(["Test1", "Test4"], ["Test1", "Test2"]), "B"=(["Test2", "Test3"], ["Test3", "Test4"])}




BiMap (or "bidirectional map") is a map with unique keys as well as values. Because of the uniqueness of this map we can interchange the key with value.
BiMap<String, String> languageCodes = HashBiMap.create();
languageCodes.put("en", "English");
languageCodes.put("fr", "French");
languageCodes.put("zh", "Chinese");

Like a normal map we can get the value by using it’s key
And now to get the key by using it’s vlaue


Popular posts from this blog

ERROR: Ignored call to 'alert()'. The document is sandboxed, and the 'allow-modals' keyword is not set.

Recently I found this issue while writing code snippet in "JSFiddle". And after searching, found this was happening because of new feature added in "Chrome 46+". But at the same time Chrome doesn't have support for "allow-modals" property in "sandbox" attribute.

Chromium issue for above behavior:

To make it work you have to add "allow-scripts allow-modals" in "sandbox" attribute, and use "window.alert" instead of "alert".

<!-- Sandbox frame will execute javascript and show modal dialogs --> <iframe sandbox="allow-scripts allow-modals" src="iframe.html"> </iframe>

Feature added: Block modal dialog inside a sandboxed iframe.

Feature working Demo page:

CSS Specificity

Many time different CSS rules overlap on one or more element. And some people always get confuse about, which rule will take higher priority then other and why? CSS Specificity is the answer of all these kind of questions.
As the name suggest, the CSS rule which is more specific to the element will take higher priority then other. Means something like “#some_id{}” will always take higher priority then “*{}” universal selector.  And if duplicate rules are define then the last rule will be applied to the element.

The following list of selectors is by increasing specificity:
Type selector (e.g., div) and pseudo-elements in selector (e.g., :after) Class selectors (e.g., .some_class), attributes selectors (e.g., [type=”radio”]) and pseudo-class selector (e.g., :hover) Id selectors (e.g., #some_id)

ID takes higher priority then Class, Type and Universal selector (Note: Universal selector has no effect on specificity, see below special conditions). 

If duplicate rules are given, then last…

JavaScript [ExtJs3]: EditorGridPanel Read-Only (dynamically)

Many time we face the scenerio where we have to make the editor grid read-only dynamically.

Ext.override(Ext.ux.grid.CheckColumn, { editable: true, onMouseDown: function (e, t) { if ( { e.stopEvent(); var me = this, grid = me.grid, view = grid.getView(), index = view.findRowIndex(t), colindex = view.findCellIndex(t), record =; if (!grid.isReadOnly && grid.colModel.isCellEditable(colindex, index)) { record.set(me.dataIndex, ![me.dataIndex]); } } } }); var grid = new Ext.grid.EditorGridPanel({ ... isReadOnly: true, //set to flag to make check column readonly ... }); //to make other column readonly grid.on('beforeedit', function () { return false; });