任何不会丢弃的类似过滤器的 lambda 操作?

Any filter-like lambda operation which does not discard?(任何不会丢弃的类似过滤器的 lambda 操作?)

本文介绍了任何不会丢弃的类似过滤器的 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 操作?

基础教程推荐