Introduction to HashMaps in Java

Welcome! Today, we're exploring the dynamic world of HashMaps in Java. It's part of the Java Collections framework, designed for efficient data storage and retrieval. Imagine a HashMap as a locker system in a spaceship, where each locker (i.e., a key) can store an item (i.e., a value). In this lesson, we'll construct a HashMap, add key-value pairs, access elements, and understand the basic properties and methods.

Ready? Let's go!

Creating HashMaps in Java

Creating a HashMap involves declaring its data type and initializing it. Just like an ArrayList, HashMaps can only hold object types. Here's a HashMap that maps our spaceship crew's names (String) to their ages (Integer).

HashMap<String, Integer> spaceshipCrew = new HashMap<>();
System.out.println(spaceshipCrew); // Prints out: {}

The HashMap is empty for now, represented by {}.

Managing Key-Value Pairs

In our spaceship, different crew members have different roles. These roles can be linked to the crew members' ages.

Adding entries to a HashMap is done with put(key, value). If put() is used with an already existing key, the old value gets replaced. remove(key) allows us to exclude entries from our HashMap.

HashMap<String, Integer> spaceshipCrew = new HashMap<>();
spaceshipCrew.put("Captain", 35); // Assigning age 35 for key "Captain"
spaceshipCrew.put("Engineer", 30); // Assigning age 30 for key "Engineer"
spaceshipCrew.put("Navigator", 32); // Assigning age 32 for key "Navigator"
System.out.println(spaceshipCrew); // Outputs {Captain=35, Engineer=30, Navigator=32}

spaceshipCrew.put("Engineer", 28); // Replaces the age of Engineer to 28
spaceshipCrew.remove("Navigator"); // Removes Navigator key (and its value too)
System.out.println(spaceshipCrew); // Outputs {Captain=35, Engineer=28}
Accessing Elements in HashMaps

Accessing a HashMap involves using the get(key) method with a unique key. This method returns the value for the specified key or null if there is no value stored for this key. For cases when you need some other default value rather than null, you can use getOrDefault(key, default) that returns a default value if the key doesn't exist.

Let's see how we can retrieve the engineer's age:

HashMap<String, Integer> spaceshipCrew = new HashMap<>();
spaceshipCrew.put("Captain", 35);
spaceshipCrew.put("Engineer", 30);
System.out.println("Engineer age: " + spaceshipCrew.get("Engineer")); // Outputs: "Engineer age: 30"
System.out.println("Artist age: " + spaceshipCrew.get("Artist")); // Outputs: "Artist age: null"
System.out.println("Artist age or default: " + spaceshipCrew.getOrDefault("Artist", 0)); // Outputs: "Artist age or default: 0"

You can see that get() for the existent key returned just the value, while for the non-existent key "Artist", it returned null. In the meantime, getOrDefault() returned the default value 0 that we provided as a parameter.

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