gpt4 book ai didi

java - 收集可能为空的值

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:04:40 26 4
gpt4 key购买 nike

我有以下代码:

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 可以为空。为了避免描述的 NPE here ,在“收集”时,我希望确保只发送那些非空值。如果为空,我想发送“”。

所以,我试着用这个替换最后一行:

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

及其其他修改:

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

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

最佳答案

您可以只收集到一个 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));
}

关于java - 收集可能为空的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48799769/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com