A very common operation on maps of collections is to create a new collection with an initial value when the key is not present, or if the key is present, do some function on the
Here's another alternative:
Set<Integer> set = m.computeIfAbsent (key , k -> new HashSet<> ());
set.add(set.size() + 1);
The only reason this is a two liner (instead of one) is the need to obtain the current size of the Set
in order to decide which value to add to it.
Not a one-liner unfortunately but it does its magic and is also directly more readable (downside : it creates a new HashSet<>()
everytime)
m.putIfAbsent(key, new HashSet<>());
// Solution 1 :
m.compute(key, (k, v) -> {v.add(v.size() + 1); return v;});
// Solution 2 :
Set<Integer> s = m.get(key);
s.add(s.size() + 1);
Or as proposed by @Thilo and inspired by @Eran
m.computeIfAbsent(key, k -> new HashSet<>()).add(m.get(key).size() + 1);
The one liner is possible because it returns the value it computed as mentioned in the javadoc
If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
There is even a similar example in the javadoc
map.computeIfAbsent(key, k -> new HashSet()).add(v);
The little trade-off of the liner is the extra call to m.get(key)
which is not happening with the solution of @Eran