Is there a more concise way to do this in Java 8 - Map manipulation

后端 未结 2 1222
迷失自我
迷失自我 2021-01-20 23:27

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

相关标签:
2条回答
  • 2021-01-21 00:12

    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.

    0 讨论(0)
  • 2021-01-21 00:20

    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

    0 讨论(0)
提交回复
热议问题