I am trying to convert List
to HashMap
using stream. Here is my code..
attributeUnitMap = attributesList.stream()
.filter(a -> a.getAttributeUnit() != null)
.collect(Collectors.toMap(Attribute::getAttributeName, a -> a.getAttributeUnit()));
Now I want to add condition that, if I get attribute name null then item should be added to map with blank string as follows (any_attributeName, "").
How can I achieve this using stream operation. I know I can check if attribute name is null by using filter condition but can I add blank string if it is null. Is it possible? if not, why so? Please help.
attributeUnitMap = attributesList.stream()
.filter(a -> a.getAttributeUnit() != null)
.collect(Collectors.toMap(
a -> a.getAttributedName() == null ? "" : a.getAttributeName(),
a -> a.getAttributeUnit()))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With