4

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.

1 Answer 1

11
attributeUnitMap = attributesList.stream()
    .filter(a -> a.getAttributeUnit() != null)
    .collect(Collectors.toMap(
        a -> a.getAttributedName() == null ? "" : a.getAttributeName(),
        a -> a.getAttributeUnit()))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.