Maps and Sets

Important Map methods

// Put value in the map under key
m.put(key, value)

// Get the value under key or null
m.get(key)

// boolean saying whether there's a value under key
m.containsKey(key)

// Make a new immutable map with keys and values
Map.of(k1, v1, k2, v2, ...)

Important Set methods

// Add a value to the set. Does nothing if already present
s.add(value)

// Return a boolean saying whether the value is present
s.contains(value)

// Make a new immutable Set containing the given values
Set.of(v1, v2, ...)

Declaring Maps and Sets

Map<String, String> m = new HashMap<>();

Set<String> s = new HashSet<>();

Set<String> keys = m.keySet();

Map<String, Integer> ss = Map.of("Fred", 18, "Sally", 17, "Suzie", 18);

Set<String> ss = Set.of("abc", "def", "ghi");