使用流操作在java中基于条件将列表转换为映射

使用流操作在java中基于条件将列表转换为映射,java,list,dictionary,java-8,java-stream,Java,List,Dictionary,Java 8,Java Stream,我正在尝试使用stream将List转换为HashMap。这是我的密码 attributeUnitMap = attributesList.stream() .filter(a -> a.getAttributeUnit() != null) .collect(Collectors.toMap(Attribute::getAttributeName, a -> a.getAttributeUnit())); 现在我想添加一个条件,若我得到属性名null,那个么应该将

我正在尝试使用stream将
List
转换为
HashMap
。这是我的密码

attributeUnitMap = attributesList.stream()
    .filter(a -> a.getAttributeUnit() != null)
    .collect(Collectors.toMap(Attribute::getAttributeName, a -> a.getAttributeUnit()));
现在我想添加一个条件,若我得到属性名null,那个么应该将项添加到带有空字符串的映射中,如下所示(any_attributeName,“”)

如何使用流操作来实现这一点。我知道我可以使用过滤条件检查属性名是否为null,但如果为null,我可以添加空白字符串。可能吗?若否,原因为何?请帮忙

attributeUnitMap = attributesList.stream()
    .filter(a -> a.getAttributeUnit() != null)
    .collect(Collectors.toMap(
        a -> a.getAttributedName() == null ? "" : a.getAttributeName(),
        a -> a.getAttributeUnit()))