Say we have a Map<Key, Collection<Value>> myMap
and a method that removes a value from the collection associated to a key. If removing a value leaves the collection empty, we'd want to get rid of the key
entry in the map:
List<Value> removeValue(Key key, Value value) {
List<Value> v = myMap.get(key);
if (v != null) {
v.remove(value);
if (v.isEmpty())
myMap.remove(key);
}
return v;
}
Is there any Java 8 way to achieve the described behavior with a one-liner or shorter method?