Menu
Coddy logo textTech

HashMap Methods

Part of the Logic & Flow section of Coddy's Java journey — lesson 16 of 59.

HashMap provides many useful methods for common operations. Here are a few important ones:

  • isEmpty(): Checks if the HashMap is empty. Returns true if it contains no key-value mappings, false otherwise.
  • size(): Returns the number of key-value mappings in the HashMap.
  • clear(): Removes all of the mappings from the HashMap. The map will be empty after this call returns.

For example let first create a HashMap:

HashMap<String, Integer> scores = new HashMap<>();

To check if the HashMap is empty:

System.out.println(scores.isEmpty());
// Output: true

If we'll add an item:

scores.put("Coddy", 100);
System.out.println(scores.isEmpty());
// Output: false

The size() method returns the number of key-value pairs in the HashMap.

System.out.println(scores.size());
// Output: 1

The clear() method removes all key-value mappings from the HashMap.

scores.clear();
System.out.println(scores.isEmpty());
// Output: true
challenge icon

Challenge

Easy

Create a method named <strong>testHashMapMethods</strong> that takes a HashMap named <strong>data</strong> as input. The method should perform the following operations:

  1. Check if the HashMap is empty and print:

    Is empty: true or Is empty: false

  2. Get the number of key-value mappings and print:

    Size: X(where <i>X</i> is the size of the HashMap).

  3. Remove all mappings from the HashMap using <strong>clear()</strong>.
  4. Check if the HashMap is empty again after clearing and print:

    Is empty after clear: true
     

Cheat sheet

Common HashMap utility methods:

  • isEmpty(): Returns true if the HashMap contains no key-value mappings
  • size(): Returns the number of key-value mappings in the HashMap
  • clear(): Removes all mappings from the HashMap
HashMap<String, Integer> scores = new HashMap<>();

// Check if empty
System.out.println(scores.isEmpty()); // true

// Add item and check again
scores.put("Coddy", 100);
System.out.println(scores.isEmpty()); // false

// Get size
System.out.println(scores.size()); // 1

// Clear all mappings
scores.clear();
System.out.println(scores.isEmpty()); // true

Try it yourself

// --- Modules to convert string of hashmap to hashmap ---
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
// -----------------------------
import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void testHashMapMethods(HashMap<String, Integer> data) {
        // Write your code here
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String hashMapString = scanner.nextLine();

        // Convert String of HashMap to HashMap
        Type mapType = new TypeToken<HashMap<String, Integer>>(){}.getType();
        HashMap<String, Integer> data = new Gson().fromJson(hashMapString, mapType);

        testHashMapMethods(data);
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow