Java Intermediate

0% completed

Previous
Next
HashMap: Operations

The HashMap class in Java provides a variety of operations to manage key-value pairs efficiently. In this lesson, we will explore common operations on a HashMap, including inserting/updating elements, removing elements, retrieving data, performing bulk operations, and iterating over the map. Each section includes an explanation, the syntax for the operation, and a code example with a bullet-point explanation.

1. Inserting and Updating Elements

In HashMap, you insert elements using the put() method. This method associates a specified value with a specified key. If the key already exists, the new value replaces the old one, and the previous value is returned.

Syntax

V put(K key, V value);
  • Explanation:
    • key: The key with which the specified value is to be associated.
    • value: The value to be associated with the key.
    • Returns the previous value associated with the key, or null if there was no mapping.

Example

Java
Java

. . . .

Example Explanation:

  • Creation:
    • A HashMap<String, Integer> called studentGrades is created.
  • Insertion:
    • The put() method adds key-value pairs: "Alice" → 85, "Bob" → 90, "Charlie" → 78.
  • Update:
    • Calling put("Alice", 88) updates Alice’s grade and returns the previous value (85).
  • Outcome:
    • The map displays the updated values, and the previous grade is printed.

2. Removing Elements

To remove an element from a HashMap, use the remove() method. You can remove an element by specifying its key. If the key exists, the method removes the key-value pair and returns the associated value.

Syntax

V remove(Object key);
  • Explanation:
    • key: The key of the element to be removed.
    • Returns the value that was associated with the key, or null if the key was not found.

Example

Java
Java

. . . .

Example Explanation:

  • Removal Operation:
    • The remove("Bob") call removes the key "Bob" and returns his grade (90).
  • Outcome:
    • The updated HashMap no longer contains "Bob", and the removed value is printed.

3. Retrieving Data

HashMap provides various methods to retrieve data:

  • get() returns the value associated with a specified key.
  • containsKey() checks if a key exists.
  • containsValue() checks if a value is present.
  • keySet(), values(), and entrySet() return views of the keys, values, and key-value pairs, respectively.

Syntax

  • Get Value:
    V get(Object key);
  • Check Key:
    boolean containsKey(Object key);
  • Key Set:
    Set<K> keySet();

Example

Java
Java

. . . .

Example Explanation:

  • Data Retrieval:
    • get("Alice") retrieves Alice’s grade.
  • Existence Check:
    • containsKey("David") checks for the key "David" and returns false.
  • Key/Value Views:
    • keySet() and values() provide sets of keys and values, respectively.
  • Outcome:
    • The retrieved values and views of keys and values are printed.

4. Bulk Operations

Bulk operations enable you to work with multiple entries at once. Common bulk operations include:

  • putAll(Map<? extends K, ? extends V> m):
    Copies all of the mappings from the specified map to this map.
  • clear():
    Removes all mappings from the map.

Syntax

  • Put All:
    void putAll(Map<? extends K, ? extends V> m);
  • Clear:
    void clear();

Example

Java
Java

. . . .

Example Explanation:

  • Merging Maps:
    • The putAll() method copies all entries from map2 into map1.
  • Clearing the Map:
    • The clear() method removes all key-value pairs from map1.
  • Outcome:
    • The merged map is printed, followed by an empty map after clearing.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next