Foundation·Concept·2 min read

Hashing and maps in Java

Practice this question

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.


Adapted from 15_DSA_JAVA.md · “3. Hashing and maps in Java”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.