HashMap + Lambda. How to add a new value <List> if it is null

I would like to "translate" this code to use it with the new Java 8. features. Lambda, map.compute.

But there is an error:

Type mismatch: cannot convert from boolean to ArrayList<DayDetailsBean>

Any ideas guys?

+4
source share
1 answer

You can use computeIfAbsent. What:

If the specified key is not yet associated with the value (or mapped to zero), it tries to calculate its value using the specified display function and enter it into this map if null is not specified.

returns the current (existing or calculated) value associated with the specified key, or null if the calculated value is null

map.computeIfAbsent(i, k -> new ArrayList<DayDetailsBean>()).add(dayDetails);
+6

Source: https://habr.com/ru/post/1658001/


All Articles