Showing posts with label Interview questions on collections. Show all posts
Showing posts with label Interview questions on collections. Show all posts

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