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
  • 1 comment:

    1. hello sir im always watching your you tube videos i am a student and i am doing my project on canvasjs charts .i need help in creading chart from databse table will you please help me.. please

      my requirement is.
      i have one table with column liike
      DateTime,var1,var2,va3,var5 and so on

      i want to create line chart
      DateTime on x axis
      and
      var1 ,var2 so on values on y axis..
      using jsp and servlets.
      in eclipse kepler.
      ..i am eagerly waiting for solution

      Thank You...

      ReplyDelete