LESSON 36 / 75·2 min readFoundation

Hashing and maps in Java

Should knowConcept lesson
01 Understand02 Recall03 Prepare

Why it matters

Hash maps are used for counts, grouping, and fast lookup.

Problems to practice

  • Valid Anagram
  • Top K Frequent Elements
  • Subarray Sum Equals K
  • First Non-Repeating Character
  • Longest Consecutive Sequence

Example: frequency count

java
Map<Character, Integer> freq = new HashMap<>();
for (char ch : s.toCharArray()) {
    freq.put(ch, freq.getOrDefault(ch, 0) + 1);
}

Interview guidance

Use HashMap when average O(1) lookup is enough. Use TreeMap when order matters.


PAUSE & RECALL

Can you explain it without the notes?

Hashing and maps in Java

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

Why it matters

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Linked list in Java

Adapted from 15_DSA_JAVA.md · “3. Hashing and maps in Java”. Examples are study fragments; framework behavior can depend on your version.