</>
Skip to content
Java lessons (38/47)

Java — HashMap

Create HashMap

import java.util.HashMap;

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

Add entries

ages.put("Alice", 25);
ages.put("Bob", 30);

Access values

int aliceAge = ages.get("Alice");
boolean hasAlice = ages.containsKey("Alice");

Remove entries

ages.remove("Bob");

Iterate

for (String name : ages.keySet()) {
    System.out.println(name + ": " + ages.get(name));
}

Mini Practice

  1. Create HashMap
  2. Add key-value pairs
  3. Access values
  4. Iterate through map

Up Next

Continue with HashSet - Unique elements.

Related Topics

Frequently Asked Questions about HashMap

What is HashMap in Java?

HashMap is a fundamental concept in Java. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn HashMap?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn HashMap.

Why is HashMap important in Java?

HashMap is essential for Java development. Understanding this concept will help you write better code and solve real-world problems more effectively.