Java HashMap class

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.

Main Point

  1. It contains only unique elements.
  2. It may have one null key and multiple null values.
  3. A HashMap contains values based on the key. It implements the Map interface and extends AbstractMap class.

Example

import java.util.*; 

	class DemoHashMap{ 

	public static void main(String args[]){ 

	

	HashMap hm=new HashMap(); 

	

	hm.put(100,"A"); 

	hm.put(101,"B"); 

	hm.put(102,"C"); 

	

	for(Map.Entry m:hm.entrySet()){ 

	System.out.println(m.getKey()+" "+m.getValue()); 

	} 

	} 

	} 

				
				
				
				
						
						
						

Output

102 C

100 A

101 B

What is difference between HashSet and HashMap?

HashSet contains only values whereas HashMap contains entry(key and value).