Wednesday, 4 June 2014

HashMap : get(Object key) : Get the Value Associated with the Key

public V get(Object key)
Returns the value to which the specified key is mapped or null if this map contains no mapping for the key.

import java.util.*;

class HashMapGet{
 public static void main(String args[]){
  HashMap<Integer, String> myMap;
  myMap = new HashMap<> ();
  
  /* Add Data to myMap */
  myMap.put(100, "Hi");
  myMap.put(25, "How");
  myMap.put(31, "Are");
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  System.out.print("Key 100 Mapped to Value ");
  System.out.println(myMap.get(100));
 }
}

Output
Elements in myMap are
{100=Hi, 25=How, 31=Are}
Key 100 Mapped to Value Hi

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment