Wednesday, 8 July 2015

Java : Collection Framework : HashMap (Single Key Multiple Values)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=ocD29fzxTVE&list=UUhwKlOVR041tngjerWxVccw

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

/**
 * HashMap - Single Key and Multiple Values using List
 */
public class SingleKeyMultipleValueUsingList
{

    public static void main( String[] args )
    {

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

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

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

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

        // put values into hashMap
        hashMap.put("A", listOne);
        hashMap.put("B", listTwo);
        hashMap.put("C", listThree);

        System.out.println("hashMap : " + hashMap +"\n");
        
        // iterate and display values
        System.out
                .println("Fetching Keys and corresponding [Multiple] Values \n");
        
        for( Map.Entry<String, List<String>> entry : hashMap.entrySet() )
        {
            String key = entry.getKey();
            List<String> values = entry.getValue();
            
            System.out.print("Key = " + key);
            System.out.println(" , Values = " + values );
        }
    }
}
Output
hashMap : {A=[Apple, Aeroplane], B=[Bat, Banana], C=[Cat, Car]}

Fetching Keys and corresponding [Multiple] Values 

Key = A , Values = [Apple, Aeroplane]
Key = B , Values = [Bat, Banana]
Key = C , Values = [Cat, Car]
To Download SingleKeyMultipleValueDemoApp Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/SingleKeyMultipleValueDemoApp.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge