【问题标题】:Collecting values that could be null收集可能为空的值
【发布时间】:2018-07-25 18:27:10
【问题描述】:

我有以下代码:

private static <T> Map<String, ?> getDifference(final T a, final T b, final Map<String, Function<T, Object>> fields) {
    return fields.entrySet().stream()
            .map(e -> {
                final String name = e.getKey();
                final Function<T, Object> getter = e.getValue();
                final Object pairKey = getter.apply(a);
                final Object pairValue = getter.apply(b);
                if (Objects.equals(pairKey, pairValue)) {
                    return null;
                } else {
                    return Pair.of(name, pairValue);
                }
            })
            .filter(Objects::nonNull)
            .collect(Collectors.toMap(Pair::getKey, Pair::getValue));
}

现在,pairValue 可以为空。为了避免如here 所述的 NPE,在“收集”时,我希望确保只发送那些非空值。如果为空,我想发送“”。

所以,我尝试将最后一行替换为:

.collect(Collectors.toMap(Pair::getKey,Optional.ofNullable(Pair::getValue).orElse(""));

及其其他修改:

.collect(Collectors.toMap(pair -> pair.getKey(), Optional.ofNullable(pair -> pair.getValue()).orElse(""));

不编译。我不确定这里需要什么。有什么帮助吗?

【问题讨论】:

  • 你能详细说明什么不起作用吗?电脑会起火吗?
  • Java 8 NullPointerException in Collectors.toMap stackoverflow.com/questions/24630963/… 这是同样的问题吗?
  • 它没有编译,编辑了问题。
  • 不幸的是,我目前无能为力。我投票决定重新提出问题并将其标记为需要主持人干预,所以我们会看看会发生什么。
  • @user2116243 好像被删除了!

标签: java java-8 hashmap java-stream key-value


【解决方案1】:

您可以只收集到HashMap,它允许null 值而不需要Optional

private static <T> Map<String, Object> getDifference(
        final T a, final T b, final Map<String, Function<T, Object>> fields) {
    return fields.entrySet().stream()
        .map(e -> {
            final Function<T, Object> getter = e.getValue();
            final Object value = getter.apply(b);
            return Objects.equals(getter.apply(a),value)? null: Pair.of(e.getKey(), value);
        })
        .filter(Objects::nonNull)
        .collect(HashMap::new, (m,p) -> m.put(p.getKey(),p.getValue()), Map::putAll);
}

顺便说一句,不鼓励在返回类型中使用通配符,它​​们会使调用者的生活变得不必要地困难而没有任何好处。

为了比较,这里没有Stream的相同操作:

private static <T> Map<String, Object> getDifference(
        final T a, final T b, final Map<String, Function<T, Object>> fields) {
    HashMap<String, Object> result = new HashMap<>();
    fields.forEach((key, getter) -> {
        final Object value = getter.apply(b);
        if(!Objects.equals(getter.apply(a), value)) result.put(key, value);
    });
    return result;
}

当然,这也适用于可选:

private static <T> Map<String, Optional<Object>> getDifference(
        final T a, final T b, final Map<String, Function<T, Object>> fields) {
    HashMap<String, Optional<Object>> result = new HashMap<>();
    fields.forEach((key, getter) -> {
        final Object value = getter.apply(b);
        if(!Objects.equals(getter.apply(a), value))
            result.put(key, Optional.ofNullable(value));
    });
    return result;
}

但如果您只想将null 替换为空字符串,则不需要Optional

private static <T> Map<String, Object> getDifference(
        final T a, final T b, final Map<String, Function<T, Object>> fields) {
    HashMap<String, Object> result = new HashMap<>();
    fields.forEach((key,getter) -> {
            final Object value = getter.apply(b);
            if(!Objects.equals(getter.apply(a), value))
                result.put(key, value==null? "": value);
        });
    return result;
}

而且,如果您只是在 map 函数而不是收集器中执行此替换,则此替换也可以直接使用您的原始代码:

private static <T> Map<String, ?> getDifference(final T a, final T b, final Map<String, Function<T, Object>> fields) {
    return fields.entrySet().stream()
        .map(e -> {
            final String name = e.getKey();
            final Function<T, Object> getter = e.getValue();
            final Object pairKey = getter.apply(a);
            final Object pairValue = getter.apply(b);
            if (Objects.equals(pairKey, pairValue)) {
                return null;
            } else {
                return Pair.of(name, pairValue==null? "": pairValue);
            }
        })
        .filter(Objects::nonNull)
        .collect(Collectors.toMap(Pair::getKey, Pair::getValue));
}

private static <T> Map<String, Object> getDifference(
        final T a, final T b, final Map<String, Function<T, Object>> fields) {
    return fields.entrySet().stream()
        .map(e -> {
            final Function<T, Object> getter = e.getValue();
            final Object pairValue = getter.apply(b);
            return Objects.equals(getter.apply(a), pairValue)? null:
                Pair.of(e.getKey(), pairValue==null? "": pairValue);
        })
        .filter(Objects::nonNull)
        .collect(Collectors.toMap(Pair::getKey, Pair::getValue));
}

【讨论】:

    【解决方案2】:

    您的语法不正确。 toMap()的第二个参数必须是lambda,所以

    .collect(Collectors.toMap(
                 pair -> pair.getKey(),
                 pair -> Optional.ofNullable(pair.getValue()).orElse("")
    ));
    

    你可以修改map()部分如下

    return Pair.of(name, Optional.ofNullable(pairValue).orElse(""));
    

    并使用您原来的collect()

    【讨论】:

      猜你喜欢
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 2018-10-17
      • 2016-07-13
      • 2017-01-28
      相关资源
      最近更新 更多