Exploring Maps and HashMap in Java

Exploring Maps and HashMap in Java

Welcome to our Java data structures revision! Today, we will delve deeply into Java Maps. Much like a bookshelf, Maps allow you to quickly select the book (value) you desire by reading its label (key). They are vital in Java for quickly accessing values using keys and for efficiently inserting and deleting keys. So, let's explore Java Maps for a clearer understanding of these concepts.

Introduction to Java Maps and Operations

Before diving into real-world applications, it’s essential to grasp the fundamentals of Java Maps, a crucial data structure for storing data as key-value pairs. Understanding how to define and perform basic operations on them prepares us for more complex implementations.

In Java, keys in a Map are unique, and you can have many different types as keys like Strings or Integers; however, if mutable objects are used as keys, their behavior can be unexpected. In most cases, developers prefer using HashMap<K, V> which allows fast lookups.

import java.util.HashMap;
import java.util.Map;

public class Solution {
    public static void main(String[] args) {
        // Defining a HashMap
        Map<String, Integer> ageMap = new HashMap<>();

        // Adding entries
        ageMap.put("Alice", 25);
        ageMap.put("Bob", 30);
        System.out.println("After adding: ");
        for (Map.Entry<String, Integer> entry : ageMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        // Updating entries
        ageMap.put("Alice", 26);  // Updates Alice's age
        System.out.println("\nAfter updating: ");
        for (Map.Entry<String, Integer> entry : ageMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        // Retrieving values
        int ageOfAlice = ageMap.containsKey("Alice") ? ageMap.get("Alice") : -1;
        System.out.println("\nAlice's Age: " + ageOfAlice);

        // Removing entries
        ageMap.remove("Bob");
        System.out.println("\nAfter removing Bob: ");
        for (Map.Entry<String, Integer> entry : ageMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        // Counting entries
        int entryCount = ageMap.size();
        System.out.println("\nNumber of Entries: " + entryCount);
    }
}
  • Defining a Map: Use HashMap<K, V> where K is the type for keys and V is the type for values.
  • Adding: Initial addition of entries, printed after insertion.
  • Updating: Demonstrates updating the existing entry by reassigning Alice's age.
  • Retrieving: Uses containsKey to check existence and retrieve values.
  • Removing: Uses the remove method to delete entries.
  • Counting: Uses the size method to get the number of entries.

Now that you're familiar with these operations, let's apply them in a PhoneBook class.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal