Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java HashMap

  1. HashMap is an in-built class which inherits AbstractMap class and implements Map interface.
  2. HashMap maintains key and value pairs and often denoted as HashMap or HashMap.
  3. A HashMap contains values based on the key.
  4. It contains only unique elements.
  5. It may have one null key and multiple null values.
  6. It maintains no order, that means it is not an ordered collection which means it does not return the keys and values in the same order in which they have been inserted into the HashMap.

HashMap class declaration

Let's see the declaration for java.util.HashMap class. public class HashMap extends AbstractMap implements Map, Cloneable, Serializable here, in this declaration :

  1. K: It is the type of keys maintained by this map.
  2. V: It is the type of mapped values.
HashMapDemo.java
import java.util.*;
class HashMapExample
{
	public static void main(String[] args)
	{
		HashMap<Integer,String> hm=new HashMap<Integer,String>();

		hm.put(1001,"John");
		hm.put(1005,"Smith");
		hm.put(1003,"Sachin");
		hm.put(1009,"James");

		for(Map.Entry m:hm.entrySet())
		{
			System.out.println(m.getKey()+" "+m.getValue());
		}
	}
}

Output

hash_output

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext