The following example shows how to get the value for a specified key
and all available values from the Map.
HashMap and Hashtable are two implementations of Map interface.
Hashtable is synchronized where as HashMap is not.
Map provides the following APIs,
public int size();
- Returns the number of entries in the Map.
public boolean isEmpty();
- Returns true if the Map contains more than one entry otherwise returns false.
public V get(Object paramObject);
- Gets value for the key, if the key exists in the Map otherwise returns null.
public Collection values();
- Returns all values from the Map as a Collection.
//Get value for the given key
//If the key present in the map then it returns the value
//otherwise returns null
System.out.println("Value for key '1' : " + numMap.get(new Integer(1)));
System.out.println("Value for key '6' : " + numMap.get(new Integer(6)));
System.out.println();
//Get all values in the map
System.out.println("Map values : " + numMap.values());
}
}
It gives the following output,
Is Map Empty ? : true
Map Size : 3
Is Map Empty ? : false
Value for key '1' : ONE
Value for key '6' : null
Map values : [ONE, TWO, THREE]