HashMap
Key–value storage with constant-time average lookups using hashing.
Intermediate
Map<String, Integer> count = new HashMap<>();
count.put("LED", 3);
count.merge("LED", 1, Integer::sum); // 4
int n = count.getOrDefault("MOTOR", 0);
for (var e : count.entrySet()) System.out.println(e.getKey() + "=" + e.getValue());
Keys must implement hashCode() and equals() consistently. Use TreeMap for sorted keys and LinkedHashMap to keep insertion order.
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.