Skip to main content

Guava: Multimap, Bimap

Multimap
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
map1
map2

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.

map1.keySet();
Output:->
["A", "B", "C", "X"]

map1.values()
Output:->
["Test1", "Test4", "Test2", "Test3", "Test5", "Test4"]

map1.entries();
Output:->
["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());

differenceMap.areEqual();
Output:->
false

differenceMap.entriesDiffering();
Output:->
{"A"=(["Test1", "Test4"], ["Test1", "Test2"]), "B"=(["Test2", "Test3"], ["Test3", "Test4"])}

differenceMap.entriesInCommon();
Output:->
{"C"=["Test5"]}

differenceMap.entriesOnlyOnLeft();
Output:->
{"X"=["Test6"]}

differenceMap.entriesOnlyOnRight();
Output:->
{"Y"=["Test6"]}

BiMap
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
languageCodes.get("en");
Output:->
"English"
And now to get the key by using it’s vlaue
languageCodes.inverse().get("English");
Output:->
"en"

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:
https://codereview.chromium.org/1126253007

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.
Link: https://www.chromestatus.com/feature/4747009953103872

Feature working Demo page:
https://googlechrome.github.io/samples/block-modal-dialogs-sandboxed-iframe/index.html



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…

Guava: Some useful IO utilities

Guava IO package provides very useful utility classes for input/ouput stream, byte stream, file handling and many more. Here are few example which show case how these utilities can make your code much cleaner, modular and more readable.Copy “InputStream” to “OutputStream InputStream is = CopyStreams.class.getResourceAsStream("test.txt"); OutputStream os = System.out; ByteStreams.copy(is, os);Changing InputStream to “byte[]” InputStream is = CopyStreams.class.getResourceAsStream("test.txt"); byte[] isBytes = ByteStreams.toByteArray(is); // Now if you want to get base64 encoded string then it will be like this String isBase64Str = new sun.misc.BASE64Encoder().encode(isBytes);Combining two files in one File input1 = new File("c:\\testio\\AWords.txt"); File input2 = new File("c:\\testio\\BWords.txt"); File output = new File("c:\\testio\\ABWords.txt"); …