How to pass Function as parameter to computeIfAbsent method?

  • A+

I am new to Java, kinda transition from C# to Java. java.util.function has a interface defined as Function which is input to computeIfAbsent method of Map.

I wanted to define and delegate that function to computeIfAbsent method.

map.computeIfAbsent(key, k => new SomeObject()) 

works but I wanted it with callback where func. But the problem is Function requires input parameter to be defined. How can I set it to void or with no argument.

map.computeIfAbsent(key, func); 


computeIfAbsent will always have an input parameter for the passed Function - that would be the key.

Therefore, just as you can write:

map.computeIfAbsent(key, k -> new SomeObject()); 

you can also write (assuming the key of your Map is a String):

Function<String,SomeObject> func = k -> new SomeObject(); map.computeIfAbsent(key, func); 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: