null values and the null key. It is unsynchronized and permits nulls. This cl"/>

HashMap Class

java.util.HashMap;
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
HashMap extends AbastractMap implements Map interface. It does not add any methods of its own. This implementation provides all of the optional map operations, and permits null values and the null key. It is unsynchronized and permits nulls. This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
The HashMap class uses a hash table to implement the Map interface. This allows the execution time of basic operations, such as get( ) and put( ), to remain constant even for large sets.
HashMap class features:
  • A HashMap cannot contain duplicate key.
  • It cantain one null key and multiple null value .
  • It does not guarantee that the insertion order will remain constant over time.
  • hashCode() and equals() methods is used for its elements .
  • Data structure is based on Hash Table .
Constructor of HashMap class
  1. public HashMap( )
  2. public HashMap(Map m)
  3. public HashMap(int capacity)
  4. public HashMap(int capacity, float fillRatio)
Example of HashMap class
					 
import java.util.HashMap; import java.util.Set; import java.util.Iterator; import java.util.Map; public class HashMapDemo { public static void main(String args[]) { HashMap hm = new HashMap(); Set set; Map.Entry m; double bal; hm.put("Sunil",134.34); hm.put("Anand", 1023.22); hm.put("Sanjeev",15678.00); hm.put("Sujeet", 5647.22); // Show all set of hash Map. set = hm.entrySet(); // get iterator Iterator it=set.iterator(); while(it.hasNext()) { m = (Map.Entry) it.next(); System.out.println(m.getKey() + ": " + m.getValue()); } System.out.println(); // Add 1350.67 into Sunil value bal = ((Double)hm.get("Sunil")).doubleValue(); hm.put("Sunil", new Double(bal+1350.67)); System.out.println("Sunil new Value: " + hm.get("Sunil")); } }
Output

Sujeet: 5647.22
Sunil: 134.34
Sanjeev: 15678.0
Anand: 1023.22

Sunil new Value: 1485.01

Download this example
 



 

Latest Tutorial


Most Visited Tutorial