Monday 19 January 2015

Java : Collection Framework : HashMap (Get)


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

HashMapExample.java
import java.util.HashMap;

/*
 * Example of get(Object key) method.
 */
public class HashMapExample
{
    public static void main( String[] args )
    {

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

        hashMap.put(1, "Apple");
        hashMap.put(2, "Ball");
        hashMap.put(3, "Cat");

        System.out.println("hashMap : " + hashMap + "\n");

        /*
         * Returns the value to which the specified key is mapped, or null if
         * this map contains no mapping for the key.
         * 
         * A return value of null does not necessarily indicate that the map
         * contains no mapping for the key; it's also possible that the map
         * explicitly maps the key to null. The containsKey operation may be
         * used to distinguish these two cases.
         */
        String value = hashMap.get(2);

        System.out.println("value for the Key '2' : " + value);

        value = hashMap.get(10);

        System.out.println("value for the Key '10' : " + value);

    }
}
Output
hashMap : {1=Apple, 2=Ball, 3=Cat}

value for the Key '2' : Ball
value for the Key '10' : null
To Download HashMapDemoGet Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/HashMapDemoGet.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment