Any filter-like lambda operation which does not discard?(任何不会丢弃的类似过滤器的 lambda 操作?)
问题描述
我基本上想做这样的事情:
I basically would like to do something like:
assertEquals(Arrays.asList(1,2,3).stream()
.noDiscardingFilter(x -> x!=1)
.map(x -> x*10)
.collect(Collectors.toList()),
Arrays.asList(1,20,30)
)
这是一个例子,我不需要回答如何解决这个特定的问题,它只是一个例子来说明我要追求什么.
This is an example, I don't need to get an answer on how to solve out that particular problem, it's just an example to show what's the fancy stuff I'm coming after.
推荐答案
任何中间步骤都会影响整个流管道.您希望 noDiscardingFilter
步骤影响随后链接的 map
将执行的操作,而不是 collect
操作,这背后没有可识别的规则.如果你想有一个条件函数,那么实现它会更清楚:
Any intermediate step affects the entire stream pipeline. There is no recognizable rule behind your wish that the noDiscardingFilter
step affects what the subsequently chained map
will do, but not the collect
operation. If you want to have a conditional function, it would be much clearer to implement it as such:
public static <T> Function<T,T> conditional(
Predicate<? super T> p, Function<T, ? extends T> f) {
return obj -> p.test(obj)? f.apply(obj): obj;
}
这可以用作
assertEquals(Stream.of(1, 2, 3)
.map(conditional(x -> x!=1, x -> x*10))
.collect(Collectors.toList()),
Arrays.asList(1, 20, 30)
);
或
Stream.of(1, 5, null, 3, null, 4)
.map(conditional(Objects::isNull, x -> 0)) // replacing null with default value
.forEach(System.out::println);
或
Stream.of(1, 5, null, 3, null, 4)
.map(conditional(Objects::nonNull, x -> x*10)) // null-safe calculation
.forEach(System.out::println);
请注意,在这些用例中,可以立即识别出传递给 conditional
的谓词和函数属于同一范围,这与链式流操作不同.
Note how in these use cases, it is immediately recognizable that the predicate and function passed to conditional
belong to the same scope, which is different from the chained stream operations.
这篇关于任何不会丢弃的类似过滤器的 lambda 操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:任何不会丢弃的类似过滤器的 lambda 操作?
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01