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中使用xpath和selenium解析HTML表格数据 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
