To know about this u can refer to this link :- http://techoschool.com/Blog/Latest/How-to-use-HashMaps-in-Java and reply me if u have any problem regarding it or you wanna suggest something !!!
Check this blog out once !
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
lakshmi priyaakaPosted Jul 3, 2015, 7:02 AM
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
packagecom.skilledmonster.examples.util.map;02.03.importjava.util.ArrayList;04.importjava.util.HashMap;05.importjava.util.List;06.importjava.util.Map;07.08./**09.* HashMap - Single Key and Multiple Values using List10.*11.* @author Jagadeesh Motamarri12.* @version 1.013.*/14.publicclassSingleKeyMultipleValueUsingList {15.16.publicstaticvoidmain(String[] args) {17.18.// create map to store19.Map> map = newHashMap>(); 20.21.// create list one and store values22.List valSetOne = newArrayList(); 23.valSetOne.add("Apple");24.valSetOne.add("Aeroplane");25.26.// create list two and store values27.List valSetTwo = newArrayList(); 28.valSetTwo.add("Bat");29.valSetTwo.add("Banana");30.31.// create list three and store values32.List valSetThree = newArrayList(); 33.valSetThree.add("Cat");34.valSetThree.add("Car");35.36.// put values into map37.map.put("A", valSetOne);38.map.put("B", valSetTwo);39.map.put("C", valSetThree);40.41.// iterate and display values42.System.out.println("Fetching Keys and corresponding [Multiple] Values n");43.for(Map.Entry> entry : map.entrySet()) { 44.String key = entry.getKey();45.List values = entry.getValue(); 46.System.out.println("Key = "+ key);47.System.out.println("Values = "+ values +"n");48.}49.}50.}