TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java ListMultimap Examples

Use the ListMultimap collection from Guava to add multiple values to keys. Call put, get and containsKey.
ListMultimap. This collection, part of com.google.common.collect, is a generic map to lists. Unlike a HashMap, we can store many values at one key.
With this class, we call put() and get() just like in a HashMap. But on multiple calls to get() with the same key, the values are all stored, not replaced. Get() returns a list.
First example. We call ArrayListMultimap.create to begin. We use the generic syntax in Java—we specify String keys and Integer (int) values.

Put: This adds a key-value pair to the ListMultimap. We can call put on the same key more than once with no error.

Get: This retrieves a list of all the values located at this key. We can loop over this with for.

For

ContainsKey: This tells us whether a key exists or not in the map. It returns true for "dog" in this program.

Java program that uses ListMultimap import java.util.List; import com.google.common.collect.*; public class Program { public static void main(String[] args) { // Create a map to ArrayList values. ListMultimap<String, Integer> map = ArrayListMultimap.create(); // Add pairs to ListMultimap. map.put("cat", 5); map.put("dog", 10); map.put("cat", 20); // Get list at this key. // ... Iterate over its values. List<Integer> list = map.get("cat"); for (int value : list) { System.out.println(value); } // See if ListMultimap contains this key. if (map.containsKey("dog")) { System.out.println(true); } } } Output 5 20 true
Remove. This method receives two arguments: a key and a value. It removes only one value in the list at a key. So the key may still have values within the ListMultimap.

Here: We add three values to the key "sky." We then remove one, the value 20. The other two values still exist at the key.

Java program that uses remove import com.google.common.collect.*; public class Program { public static void main(String[] args) { // Create a ListMultimap and add data to it. ListMultimap<String, Integer> map = ArrayListMultimap.create(); map.put("lawn", 10); map.put("sky", 50); // Add three values at sky. map.put("sky", 20); map.put("sky", 100); // Remove one of the values at sky. map.remove("sky", 20); // Sky now has just two values. System.out.println(map); } } Output {sky=[50, 100], lawn=[10]}
Nonexistent key. What happens when we call get() but no values exist at that key? In this example we find an empty list is returned. It has zero elements, but is not null.

Tip: To see if a key exists, please use containsKey. This makes code clearer and easier to understand.

Java program that gets nonexistent key import java.util.List; import com.google.common.collect.*; public class Program { public static void main(String[] args) { // Create an empty ListMultimap. ListMultimap<String, Integer> map = ArrayListMultimap.create(); // It is empty. System.out.println(map.isEmpty()); // Remove one of the values at sky. List<Integer> list = map.get("Hyperion"); // A nonexistent value list is empty, not null. System.out.println(list); System.out.println(list.isEmpty()); System.out.println(list.size()); } } Output true [] true 0
With ListMultimap, we have a normal map collection. But at each key, many values may exist. We can add these values just with the key and put(). No special logic is required.
As with other Guava classes, this collection is a prebuilt solution for a common problem. I find multimaps are less often used than things like HashMap. But this need is not rare.guava: github.com
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf