Javatpoint Logo

91-9990449935

 0120-4256464

Javatpoint Logo

Java HashMap class

Java HashMap class hierarchy

Java HashMap class implements the map interface by using a hashtable. It inherits AbstractMap class and implements Map interface.

The important points about Java HashMap class are:

  • A HashMap contains values based on the key.
  • It contains only unique elements.
  • It may have one null key and multiple null values.
  • It maintains no order.

Hierarchy of HashMap class

As shown in the above figure, HashMap class extends AbstractMap class and implements Map interface.

HashMap class declaration

Let's see the declaration for java.util.HashMap class.

HashMap class Parameters

Let's see the Parameters for java.util.HashMap class.

  • K: It is the type of keys maintained by this map.
  • V: It is the type of mapped values.

Constructors of Java HashMap class

ConstructorDescription
HashMap()It is used to construct a default HashMap.
HashMap(Map m)It is used to initializes the hash map by using the elements of the given Map object m.
HashMap(int capacity)It is used to initializes the capacity of the hash map to the given integer value, capacity.
HashMap(int capacity, float fillRatio)It is used to initialize both the capacity and fill ratio of the hash map by using its arguments.

Methods of Java HashMap class

MethodDescription
void clear()It is used to remove all of the mappings from this map.
boolean containsKey(Object key)It is used to return true if this map contains a mapping for the specified key.
boolean containsValue(Object value)It is used to return true if this map maps one or more keys to the specified value.
boolean isEmpty()It is used to return true if this map contains no key-value mappings.
Object clone()It is used to return a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
Set entrySet()It is used to return a collection view of the mappings contained in this map.
Set keySet()It is used to return a set view of the keys contained in this map.
Object put(Object key, Object value)It is used to associate the specified value with the specified key in this map.
int size()It is used to return the number of key-value mappings in this map.
Collection values()It is used to return a collection view of the values contained in this map.

Java HashMap Example

Test it Now
Output:102 Rahul
       100 Amit
       101 Vijay

Java HashMap Example: remove()

Output:

Values before remove: {102=Operating System, 103=Data Communication and Networking, 101=Let us C}
Values after remove: {103=Data Communication and Networking, 101=Let us C}

Difference between HashSet and HashMap

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

Java HashMap Example: Book

Output:

1 Details:
101 Let us C Yashwant Kanetkar BPB 8
2 Details:
102 Data Communications & Networking Forouzan Mc Graw Hill 4
3 Details:
103 Operating System Galvin Wiley 6




Share this page

facebook twitter google plus pinterest tumbler

Latest 4 Tutorials