在Java8中向映射添加非值的优雅方式?

在Java8中向映射添加非值的优雅方式?,java,hashmap,java-8,guava,Java,Hashmap,Java 8,Guava,我使用的是不可变映射 public Map<String,String> getMap(){ return ImmutableMap.<String,String>builder() .put("FOO",getFooType()) .put("BAR", getBarType()) .build(); } 由于我有许多键要添加到地图中,这种if检查使

我使用的是不可变映射

public Map<String,String> getMap(){
        return ImmutableMap.<String,String>builder()
                .put("FOO",getFooType())
                .put("BAR", getBarType())
                .build();
    }
由于我有许多键要添加到地图中,这种if检查使代码不美观。我想知道是否有什么优雅的方法可以做到这一点


我正在为我的项目使用Java 8。

您可以使用
可选的
作为映射的值:

public Map<String,Optional<String>> getMap(){
  return ImmutableMap.<String,Optional<String>>builder()
    .put("FOO",Optional.<String>ofNullable(getFooType()))
    .put("BAR", Optional.<String>ofNullable(getBarType()))
    .build();
}
publicmap getMap(){
返回ImmutableMap.builder()
.put(“FOO”,可选.ofNullable(getFootType())
.put(“BAR”,可选.ofNullable(getBarType())
.build();
}
这样,映射将存储包装字符串的可选对象,当您从映射中获取值时,使用
map.get(key).orElse(DEF_值)-这将为那些具有空值的对象提供DEF_值

更多信息请参见重复的

if (fooType!=null) {
    map.put("FOO", fooType);
}
看起来冗长,因为它们是重复的。如果您只是将条件add操作放入一个方法中并重用它,那么代码将与初始的非条件代码一样紧凑,因为它包含每个所需映射的一个方法调用

请注意,您可以轻松地将其与番石榴方法相结合:

class MyBuilder<K,V> extends ImmutableMap.Builder<K,V> {
    public MyBuilder<K, V> putIfValueNotNull(K key, V value) {
        if(value!=null) super.put(key, value);
        return this;
    }
}
类MyBuilder扩展了ImmutableMap.Builder{ 公共MyBuilder putIfValueNotNull(K键,V值){ if(value!=null)super.put(key,value); 归还这个; } }

publicmap getMap(){
返回新的MyBuilder()
.putIfValueNotNull(“FOO”,getFooType())
.putIfValueNotNull(“BAR”,getBarType())
.build();
}
如果您喜欢这种编码风格,可以将
MyBuilder
创建包装到
builder()
类型的工厂方法中。

纯Java 8解决方案:

public Map<String, String> getMap() {
    return Stream.of(
            new AbstractMap.SimpleEntry<>("FOO", getFooType()),
            new AbstractMap.SimpleEntry<>("BAR", getBarType())
    )
            .filter(entry -> entry.getValue() != null)
            .filter(entry -> !entry.getValue().isEmpty())
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
publicmap getMap(){
回流(
新的AbstractMap.SimpleEntry(“FOO”,getFootType()),
新的AbstractMap.SimpleEntry(“BAR”,getBarType())
)
.filter(entry->entry.getValue()!=null)
.filter(条目->!entry.getValue().isEmpty())
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
}

这些
getFootType
getBarType
是静态方法?进入地图的项目的来源是什么?它们不是静态方法。要映射的项来自方法调用。该方法应返回map是否需要该映射不可变?是一个
集合。不可修改的映射也可以吗?很好。想知道
getMap()
是否可以返回
Map
Map
上做一些后处理?看看
MapUtils
(不记得是番石榴还是apacheCommons)-你可以使用filter过滤掉那些值为空可选值的,然后使用convert-to-grt从其余选项中提取值。(但如果你问我,如果可能的话,请留下所有选项)
public Map<String,String> getMap(){
    return new MyBuilder<String,String>()
            .putIfValueNotNull("FOO",getFooType())
            .putIfValueNotNull("BAR", getBarType())
            .build();
}
public Map<String, String> getMap() {
    return Stream.of(
            new AbstractMap.SimpleEntry<>("FOO", getFooType()),
            new AbstractMap.SimpleEntry<>("BAR", getBarType())
    )
            .filter(entry -> entry.getValue() != null)
            .filter(entry -> !entry.getValue().isEmpty())
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}