HashMap
HashMap stores key value pairs. put, get, getOrDefault, containsKey and entrySet.
Example
import java.util.*;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> m = new HashMap<>();
m.put("a", 1); m.put("b", 2);
System.out.println(m.get("a") + " " + m.getOrDefault("z", 0) + " " + m.containsKey("b"));
for (var e : new TreeMap<>(m).entrySet()) System.out.println(e.getKey() + "=" + e.getValue());
}
}Result
1 0 true a=1 b=2
Exercise
Read by key.
m.("a")
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.