[Q#19278443][A#19279560] How do I use the new computeIfAbsent function?
https://stackoverflow.com/q/19278443
I very much want to use Map.computeIfAbsent but it has been too long since lambdas in undergrad. Almost directly from the docs: it gives an example of the old way to do things: And the new way: But in their example, I think I'm not quite "getting it." How would I transform the code to use the new lambda way of expressing this?
Answer
https://stackoverflow.com/a/19279560
Suppose you have the following code: Then you will see the message creating a value for "snoop" exactly once as on the second invocation of computeIfAbsent there is already a value for that key. The k in the lambda expression k -> f(k) is just a placeolder (parameter) for the key which the map will pass to your lambda for computing the value. So in the example the key is passed to the function invocation. Alternatively you could write: whoLetDogsOut.computeIfAbsent("snoop", k -> k.isEmpty()); to achieve the same result without a helper method (but you won’t see the debugging output then). And even simpler, as it is a simple delegation to an existing method you could write: whoLetDogsOut.computeIfAbsent("snoop", String::isEmpty); This delegation does not need any parameters to be written. To be closer to the example in your question, you could write it as whoLetDogsOut.computeIfAbsent("snoop", key -> tryToLetOut(key)); (it doesn’t matter whether you name the parameter k or key). Or write it as whoLetDogsOut.computeIfAbsent("snoop", MyClass::tryToLetOut); if tryToLetOut is static or whoLetDogsOut.computeIfAbsent("snoop", this::tryToLetOut); if tryToLetOut is an instance method.
APIzation
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Test {
public static void main(String[] s) {
Map<String, Boolean> whoLetDogsOut = new ConcurrentHashMap<>();
whoLetDogsOut.computeIfAbsent("snoop", k -> f(k));
whoLetDogsOut.computeIfAbsent("snoop", k -> f(k));
}
static boolean f(String s) {
System.out.println("creating a value for \""+s+'"');
return s.isEmpty();
}
}
package com.stackoverflow.api;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Human19279560 {
public static void usageOfComputeIfAbsent() {
Map<String, Boolean> whoLetDogsOut = new ConcurrentHashMap<>();
whoLetDogsOut.computeIfAbsent("snoop", k -> f(k));
whoLetDogsOut.computeIfAbsent("snoop", k -> f(k));
}
static boolean f(String s) {
System.out.println("creating a value for \"" + s + '"');
return s.isEmpty();
}
}
package com.stackoverflow.api;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* How do I use the new computeIfAbsent function?
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/19279560">https://stackoverflow.com/a/19279560</a>
*/
public class APIzator19279560 {
public static void useFunction() {
Map<String, Boolean> whoLetDogsOut = new ConcurrentHashMap<>();
whoLetDogsOut.computeIfAbsent("snoop", k -> f(k));
whoLetDogsOut.computeIfAbsent("snoop", k -> f(k));
}
static boolean f(String s) {
System.out.println("creating a value for \"" + s + '"');
return s.isEmpty();
}
}