Lesson 4

Navigating through Java's HashMaps: Keys to Efficient Data Storage

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).

Java
1HashMap<String, Integer> spaceshipCrew = new HashMap<>(); 2System.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.

Java
1HashMap<String, Integer> spaceshipCrew = new HashMap<>(); 2spaceshipCrew.put("Captain", 35); // Assigning age 35 for key "Captain" 3spaceshipCrew.put("Engineer", 30); // Assigning age 30 for key "Engineer" 4spaceshipCrew.put("Navigator", 32); // Assigning age 32 for key "Navigator" 5System.out.println(spaceshipCrew); // Outputs {Captain=35, Engineer=30, Navigator=32} 6 7spaceshipCrew.put("Engineer", 28); // Replaces the age of Engineer to 28 8spaceshipCrew.remove("Navigator"); // Removes Navigator key (and its value too) 9System.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:

Java
1HashMap<String, Integer> spaceshipCrew = new HashMap<>(); 2spaceshipCrew.put("Captain", 35); 3spaceshipCrew.put("Engineer", 30); 4System.out.println("Engineer age: " + spaceshipCrew.get("Engineer")); // Outputs: "Engineer age: 30" 5System.out.println("Artist age: " + spaceshipCrew.get("Artist")); // Outputs: "Artist age: null" 6System.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.

Exploring HashMap Properties & Methods

HashMaps provide useful methods such as:

  • size(): returns the count of key-value pairs
  • clear(): clears all key-value pairs
  • containsKey(): check if a certain key exists in the map

Let's show these in the following example:

Java
1HashMap<String, Integer> spaceshipCrew = new HashMap<>(); 2spaceshipCrew.put("Captain", 35); 3spaceshipCrew.put("Engineer", 30); 4System.out.println("Crew size: " + spaceshipCrew.size()); // Outputs: "Crew size: 2" 5System.out.println("Do we have a Navigator? " + spaceshipCrew.containsKey("Navigator")); 6// Outputs: "Do we have a Navigator? false"
Lesson Summary

Congratulations! You've navigated through HashMaps in Java: their creation, addition, removal, and the accessing of elements. We also understood the properties and methods of HashMap. Coming up next are practice exercises to solidify today's concepts. Keep going!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.