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:

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

JavaScript [ExtJs3]: Total “Record” count in filtered store

There is two way to get record count from the Store
store.getTotalCount() This function depend on server response value. For accuracy of the value, property shell if return by the server.

Property name for the diff. reader:
totalProperty for JsonReader, totalRecords for XmlReaderstore.getCount() Will return you the number of record from the store.
Or if you have filter on the store, it will give you the number of filtered record.
But if you want to get the total number of record regardless filtering, Then it will be like this

var totalRecords = store.snapshot ? store.snapshot.length : store.getCount();
“snapshot” is the variable in “Store” which hold the actual data in case if you have applied a filter.