4 minutes read

The HashMap class in Java is a member of the Java Collections Framework and is implemented as a hash table. This means that it stores data in key-value pairs, where each key is unique and is used to retrieve the corresponding value. The basic operations of insertion, retrieval and deletion are performed in an average time of O(1), which makes the HashMap a very efficient data structure for many types of applications.

This allows the execution of get() and put() methods to remain constant even for large sets. You should remember that HashMap does not guarantee the order of its elements — the order in which the elements are added may or may not be same.

How to create a HashMap

First, you need to import the HashMap package in your Java program:

import java.util.HashMap;

Then, at the declaration point inside the method you need to declare it as follow:

HashMap<K, V> map = new HashMap<>();

Here, K and V are types of key and value.

HashMap methods

  • get(K): returns the value associated with the given key, or null if the key is not present in the map. For example:

HashMap<String, Integer> myMap = new HashMap<>();
myMap.put("apple", 5);
myMap.put("banana", 3);
int appleCount = myMap.get("apple"); // appleCount will be 5
  • getOrDefault(K,V): returns the value associated with the given key, or the default value if the key is not present in the map. For example:

HashMap<String, Integer> myMap = new HashMap<>();
myMap.put("apple", 5);
int bananaCount = myMap.getOrDefault("banana", 0); // bananaCount will be 0
  • put(K,V): inserts the specified key-value pair into the map. If the key already exists, the existing value is replaced with the new one. For example:

HashMap<String, Integer> myMap = new HashMap<>();
myMap.put("apple", 5);
myMap.put("apple", 7); // replaces the existing value of 5 with 7 for the key "apple"
  • remove(K): removes the key-value pair associated with the given key from the map. For example:

HashMap<String, Integer> myMap = new HashMap<>();
myMap.put("apple", 5);
myMap.put("banana", 3);
myMap.remove("apple"); // removes the key-value pair associated with the key "apple"
  • size(): returns the number of key-value pairs in the map. For example:

HashMap<String, Integer> myMap = new HashMap<>();
myMap.put("apple", 5);
myMap.put("banana", 3);
int mapSize = myMap.size(); // mapSize will be 2
  • containsKey(K): returns true if the map contains the specified key, false otherwise. For example:

HashMap<String, Integer> myMap = new HashMap<>();
myMap.put("apple", 5);
boolean hasApple = myMap.containsKey("apple"); // hasApple will be true
  • containsValue(V): returns true if the map contains the specified value, false otherwise. For example:

HashMap<String, Integer> myMap = new HashMap<>();
myMap.put("apple", 5);
myMap.put("banana", 3);
boolean hasValue3 = myMap.containsValue(3); // hasValue3 will be true
  • keySet(): returns a set view of the keys contained in the map. For example:

HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);

// Print all keys
System.out.println("Keys: " + map.keySet()); 
// Output: Keys: [apple, banana, orange]
  • values(): returns a collection view of the values contained in the map. For example:

HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);

// Print all values
System.out.println("Values: " + map.values());
// Output: Values: [1, 2, 3]
  • entrySet(): returns a set view of the mappings contained in the map. For example:

HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);

// Print all key-value pairs
System.out.println("Entries: " + map.entrySet());
// Output: Entries: [apple=1, banana=2, orange=3]

Limitations

Although HashMap is a useful data structure for handling key-value storage, it still has several drawbacks:

  • HashMap keys must be distinct; if you attempt to insert a key with the same value more than once, the previous value will be replaced. HashMap provides many methods of O(1) time complexity, but containsValue(V) has the O(n) time complexity.

  • The order of item insertion of the items is not preserved by HashMap.

  • Standard HashMap key classes include String, Character, Integer, Long, and Boolean. When using custom classes as keys, it is important to override the equals() and hashCode() methods correctly to ensure proper behavior. If you don't do it, it may result in losing elements from the map, or in poor performance due to collisions in the hash table.

Conclusion

The HashMap class in Java is a powerful and efficient data structure for implementing key-value storage. It provides a wide range of methods for working with data, including insertion, retrieval, deletion, and querying. The basic operations have an average time complexity of O(1), making it suitable for use in many types of applications. However, when using custom classes as keys, it is important to override the equals() and hashCode() methods correctly to ensure proper behavior. Overall, HashMap is a great choice for many use cases where fast key-value storage is required.

30 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo