So I found a curiosity with Java's Map computeIfAbsent
(using java8) method and I hope someone can tell me why this is happening as I can not really follow the logic behind that issue.
So, I have a Map with a key (obviously) and the value is a list and I use the computeIfAbsent to create a new list when a key is not set yet. Now when I use an Integer as key I can use the following:
List<Object> list = map.computeIfAbsent(1, ArrayList::new);
But when I use a String as key trying to use
List<Object> list = map.computeIfAbsent("key", ArrayList::new);
I get the error that The method computeIfAbsent(String, Function<? super String,? extends List<Object>>) in the type Map<String,List<Object>> is not applicable for the arguments (String, ArrayList::new)
. Is there just the implementation missing? Using a String key I have to use the method like that, which is then working again.
List<Object> list = map.computeIfAbsent("key", k -> new ArrayList<>());
Maybe someone can enlighten me about that. Thanks :)