Skip to content
Learn Netverks

Lesson

Step 20/36 56% through track

hashmap

HashMap

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches HashMap: the syntax, APIs, and habits you need before advancing in Java.

Teams ship HashMap on every Java codebase—skipping it leaves gaps in debugging and code reviews.

You will apply HashMap in contexts like: In-memory models, caches, and DTO mapping in APIs and batch jobs.

Write Java with a public class (lessons use Main), click Run on server—the dev runner runs javac then java; fix compile errors from stderr (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

HashMap<K,V> stores key-value pairs with O(1) average lookup. Keys should implement consistent equals and hashCode.

Basic usage

HashMap<String, Integer> counts = new HashMap<>();
counts.put("a", 1);
counts.get("a");
counts.containsKey("a");
counts.getOrDefault("z", 0);

Iteration

for (var entry : counts.entrySet()) {
    System.out.println(entry.getKey() + "=" + entry.getValue());
}

Important interview questions and answers

  1. Q: HashMap vs TreeMap?
    A: HashMap unordered O(1) average; TreeMap sorted keys O(log n).
  2. Q: Is HashMap thread-safe?
    A: No—use ConcurrentHashMap for concurrent access.

Self-check

  1. What method retrieves a value by key?
  2. Why must key objects implement hashCode?

Interview prep

Why equals and hashCode together?

Hash-based collections bucket by hash code then compare with equals—equal keys must share hash codes or lookups fail.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Key equals contract?
  • HashMap vs TreeMap?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump