Sunday, June 11, 2017

HashMap – Single Key and Multiple Values Example

Sometimes you want to store multiple values for the same hash key. The following code examples show you 2 different ways to do this.


Scenario:

HashMap can be used to store key-value pairs.

But sometimes you may want to store multiple values for the same key.

For example:
For Key A, you want to store - Apple, Aeroplane

For Key B, you want to store - Bat, Banana

For Key C, you want to store – Cat, Car

The following code snippets will show you at least 2 different ways of storing key-value pairs with a distinction of Single Key and Multiple Values.

HashMap - Single Key and Multiple Values Using List

Sample Code Snippet:

private Map<String, List<String>> mapList;

mapList = new HashMap<String, List<String>>();

public void puStringList(String key, List stringList) {
      mapList.put(key, stringList);

}
    

public List getStringList(String key) {
        return mapList.get(key);

}

HashMap – Single Key and Multiple Values Using Google Guava Collections

Maven dependency for Google Guava Collections:

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>19.0</version>
</dependency>


Sample Code Snippet:

private Multimap<String, String> multiMap;

multiMap = ArrayListMultimap.create();

public void putMultiMap(String key, String value) {
    multiMap.put(key, value);
   
}

public List<String> getMultiMap(String key) {
    return (List<String>) multiMap.get(key);
}


Full Code using a class Map2Multiple combining both 2 ways.


package com.map.utility;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class Map2Multiple {
  
    private Map<String, List<String>> mapList;
    private Multimap<String, String> multiMap;
  
  
    public Map2Multiple() {
        mapList = new HashMap<String, List<String>>();
        multiMap = ArrayListMultimap.create();
    }
  
    public void puStringList(String key, List<String> stringList) {
        mapList.put(key, stringList);
    }
  
    public List<String> getStringList(String key) {
        return mapList.get(key);
    }
  
    public void putMultiMap(String key, String value) {
        multiMap.put(key, value);
      
    }
  
    public List<String> getMultiMap(String key) {
        return (List<String>) multiMap.get(key);
    }
  
    public void printAllMapList() {
        for(Map.Entry<String,List<String>> entry : mapList.entrySet())
        {
            System.out.println("key:    " + entry.getKey());
            System.out.println("Value:    " + entry.getValue());
        }
    }
  
    public void printAllMultiMap() {
        for(String key : multiMap.keySet())
        {
            System.out.println("key:    " + key);
            System.out.println("Value:    " + multiMap.get(key));
        }
      
    }

}

Test Map2Multiple from main method of Map2MultipleMain.


 package com.map.utility;

import java.util.Arrays;

public class Map2MultipleMain {

    public static void main(String[] args) {
        Map2Multiple maps = new Map2Multiple();
       
        maps.puStringList("A", Arrays.asList("Apple", "Aeroplane", "Army"));
        maps.puStringList("A", Arrays.asList("Angry", "Aeroplane", "Army"));
       
        System.out.println(maps.getStringList("A"));
       
        maps.putMultiMap("B", "Banana");
        maps.putMultiMap("B", "Bat");
        maps.putMultiMap("B", "Ball");
        maps.putMultiMap("B", "Ballon");
        maps.putMultiMap("C", "Cat");
        maps.putMultiMap("C", "Car");
        maps.putMultiMap("C", "Canada");
       
        System.out.println(maps.getMultiMap("Z"));
       
        maps.printAllMapList();
        maps.printAllMultiMap();

    }

}

No comments:

Post a Comment