Java HashMap Example

Arrays’ items are stored as an ordered collection and we can access them by indices. HashMap class in Java on the other hand, stores items in a group pairs, key/value. They can be accessed by an index of another type. This class does not guarantee that there will be a constant order over time. HashMap provides constant-time performance for basic operations such as get and put, assuming it is implemented correctly. Just like HashSet class, HashMap has initial capacity and load factor. The capacity is the number of buckets in the hash table and load factor is just a measure of how full the hash table is allowed to get before its capacity is automatically increased. Like HashSet, the default load factor is 0.75.

java-featured-image

Why is HashMap important and useful

  • Due to its key/value pairs, it is easy to organize data.
  • HashMap allows 1 null key and multiple null values.
  • HashMap does not allow duplicate keys but allows duplicate values.
  • HashMap extends an abstract class AbstractMap.




Inheritance Diagram

Inheritance diagram of HashMap

Constructors summary in HashMap

  1. HashMap(): initializes an empty HashMap with the default initial capacity, 16, and with the default load factor, 0.75.
  2. HashMap(int initialCapacity): initializes an empty HashMap with the specified initial capacity and with the default load factor, 0.75.
  3. HashMap(int initialCapacity, float loadFactor): initializes an empty HashMap with the specified initial capacity and load factor.
  4. HashMap(Map<? extends K, ? extends V> m): initializes a new HashMap with the same mappings as the specified Map.

Methods in HashMap class

  1. void clear(): removes all of the mappings from this map.
  2. Object clone(): clones another HashMap, the keys and values themselves are not cloned, however.
  3. boolean containsKey(Object key): returns true if key is in the hash map, false otherwise.
  4. boolean containsValue(Object value): reutrns true if value is in in any of the keys in the hash map, false otherwise.
  5. V get(Object key): Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
  6. boolean isEmpty(): returns true if the map contains no elements.
  7. V put(K key, V value): adds the specified value to the specified key.
  8. V remove(Object key): removes the key from the hash map.
  9. V replace(K key, V value): replaces the entry for the specified key only if it is currently mapped to some value.
  10. int size(): returns the number of key-value pairs in this map.

Basic method manipulation with HashMap and how they work and  interact with each other

import java.util.HashMap;

public class HashMapExample {
  public static void main(String[] args) {
    HashMap<String, String> animals = new HashMap<String, String>();
    
    // putting a key-value pairs within a HashMap
    // animal -> name
    animals.put("Elephant", "Dicky");
    animals.put("Tiger", "Sammy");
    animals.put("Lion", "Sim");

    System.out.println(animals); 

    // accessing an item using get()
    // gives back the value to the specified key, which means it will return back "Sim"
    System.out.println("The name of 'Lion' is: " + animals.get("Lion");

    // removing an item using remove()
    animals.remove("Elephant");

    // getting the size of the hash map
    System.out.println("The size of the hash map before clearing: " + animals.size());

    // clearing/deleting a whole hash map using clear()
    animals.clear()

    // getting the size of the hash map
    System.out.println("The size of the hash map after clearing: " + animals.size());
  }
}

Output:

[Lion=Sam, Tiger=Sammy, Elephant=Dicky]
The name of 'Lion' is: Sam
The size of the hash map before clearing: 2
The size of the hash map after clearing: 0

 

Leave a Reply

avatar