<aside> πŸ’‘ Key-value structures comes under the Map Interface.

</aside>

public interface Map<K,V>{
	V get(Object key);
	V put(K key, V value);

	boolean containsKey(Object key);
	boolean containsValue(Object value);
	...
}

Updating a Map

  1. Map has the following default method β†’ V getDefault(Object key,V defaultValue);


  2. Can Use putIfAbsent() to initialize a missing key.

    scores.putIfAbsent(bat,0); //Sets the value of key bat to 0 if the key does not exist, else do nothing.
    scores.put(bat, scores.get(bat)+newscore);
    // now update value with newscore because the key bat is guaranteed to have a value(either 0 or
    // the previously stored value)
    

  3. Or use merge()

    score.merge(bat,newscore, Integer::sum);
    

Extracting Keys and Values