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

2022-01-22 00:00:00 lambda java-8 java java-stream

我基本上想做这样的事情:

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.

相关文章