HashMap

A hash table based implementation of the Map interface. Stores key-value pairs with O(1) average put/get.

Syntax

java
HashMap<KeyType, ValueType> map = new HashMap<>();

Example

java
HashMap<String, Integer> scores = new HashMap<>();
scores.put("Alice", 95);
scores.put("Bob", 87);
System.out.println(scores.get("Alice")); // 95
scores.getOrDefault("Carol", 0);         // 0