How can we assign multiple values to a single key in HashMap?

HashMap can be used to store values in key-value pair format, but some times you might be want store multiple values for the same key like
for key A, you want to store Apple and America
for key B, you want to store Bat and Bangladesh
for key C, you want to store Cat and China
the following code snippet will show how to store multiple values for the same key.

HashMap-single key multiple values using LIST

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SingleKeyMultipleValuesUsingList
{
public static void main(String[] args)
{
// create map to store
Map<String, List<String>> map = new HashMap<String, List<String>>();

               // create list one and store values
List<String> l1 = new ArrayList<String>();
l1.add("Apple");
l1.add("America");

               // create list two and store values
List<String> l2 = new ArrayList<String>();
l2.add("Bat");
l2.add("Bangladesh");

               // create list three and store values
List<String> l3 = new ArrayList<String>();
l3.add("Cat");
l3.add("China");

               // put values into map
map.put("A", l1);
map.put("B", l2);
map.put("C", l3);

               // iterate and display values
System.out.println("Fetching Keys and corresponding MULTIPLE Values n");
for (Map.Entry<String, List<String>> entry : map.entrySet())
{
String key = entry.getKey();
List<String> values = entry.getValue();
System.out.println("Key = " + key);
System.out.println("Values = " + values + "/n");
}
}
}

HashMap-single key multiple values using Collections

import java.util.Set;
import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.map.MultiValueMa;

public class SingleKeyMultipleValuesUsingApacheCollections
{
public static void main(String[] args)
{
// create multimap to store key and values
MultiMap multiMap = new MultiValueMap();

                // put values into map for A
multiMap.put("A", "Apple");
multiMap.put("A", "America");

                // put values into map for B
multiMap.put("B", "Bat");
multiMap.put("B", "Bangladesh");

                // put values into map for C
multiMap.put("C", "Cat");
multiMap.put("C", "China");

                // retrieve and display values
System.out.println("Fetching Keys and corresponding MULTIPLE Values n");

                // get all the set of keys
Set<String> keys = multiMap.keySet();

                // iterate through the key set and display key and values
for (String key : keys)
{
System.out.println("Key = " + key);
System.out.println("Values = " + multiMap.get(key) + "/n");
}
}
}

2 comments:

  1. how to fetch a specific value from a key that has multiple values in it? kindly help me out.
    eg :
    map.get("key1")--> prints "apple","banana"
    but i want to fetch a specific value

    ReplyDelete
    Replies
    1. updating the question..
      how to fetch a specific value from a key that has multiple values in it? kindly help me out.
      eg :
      map.get("key1")--> prints "apple","banana"
      but i want to fetch a specific value ..lets say i want to fetch "banana" only

      Delete