我正在阅读有关Java流的资料,并在阅读过程中发现新的东西。我发现的一个新东西是peek()函数。几乎所有我读到的peek说它应该用来调试你的流。

如果我有一个流,其中每个帐户都有一个用户名,密码字段和login()和loggedIn()方法。

我还有

Consumer<Account> login = account -> account.login();

and

Predicate<Account> loggedIn = account -> account.loggedIn();

为什么会这么糟糕?

List<Account> accounts; //assume it's been setup
List<Account> loggedInAccount = 
accounts.stream()
    .peek(login)
    .filter(loggedIn)
    .collect(Collectors.toList());

现在,据我所知,这完全是它的目的。它;

获取一个帐户列表 尝试登录每个帐户 过滤掉任何未登录的帐户 将已登录的帐户收集到一个新列表中

这样做的坏处是什么?有什么理由不让我继续吗?最后,如果不是这个解决方案,那么会是什么?

它的原始版本使用.filter()方法如下所示;

.filter(account -> {
        account.login();
        return account.loggedIn();
    })

当前回答

为了消除警告,我使用了函数子tee,以Unix的tee命名:

public static <T> Function<T,T> tee(Consumer<T> after) {
    return arg -> {
        f.accept(arg);
        return arg;
    };
}

你可以替换:

  .peek(f)

with

  .map(tee(f))

其他回答

您必须了解的重要一点是,流是由终端操作驱动的。终端操作决定是否必须处理所有元素,或者是否必须处理任何元素。因此collect是处理每个项的操作,而findAny在遇到匹配元素时可能会停止处理项。

And count() may not process any elements at all when it can determine the size of the stream without processing the items. Since this is an optimization not made in Java 8, but which will be in Java 9, there might be surprises when you switch to Java 9 and have code relying on count() processing all items. This is also connected to other implementation-dependent details, e.g. even in Java 9, the reference implementation will not be able to predict the size of an infinite stream source combined with limit while there is no fundamental limitation preventing such prediction.

Since peek allows “performing the provided action on each element as elements are consumed from the resulting stream”, it does not mandate processing of elements but will perform the action depending on what the terminal operation needs. This implies that you have to use it with great care if you need a particular processing, e.g. want to apply an action on all elements. It works if the terminal operation is guaranteed to process all items, but even then, you must be sure that not the next developer changes the terminal operation (or you forget that subtle aspect).

此外,虽然流保证维持某些操作组合的遇到顺序,即使是并行流,但这些保证并不适用于peek。当收集到列表中时,结果列表将具有有序并行流的正确顺序,但peek操作可能以任意顺序并发调用。

因此,你可以用peek做的最有用的事情是找出一个流元素是否被处理了,这正是API文档所说的:

此方法的存在主要是为了支持调试,在调试中,您希望看到元素流经管道中的某个点时的情况

我想说的是,peek提供了去中心化代码的能力,这些代码可以改变流对象,或修改全局状态(基于它们),而不是将所有东西都塞进一个简单的或组合的函数中,传递给终端方法。

现在的问题可能是:在函数式java编程中,我们应该改变流对象还是从函数内部改变全局状态?

如果以上两个问题中的任何一个的答案是肯定的(或者:在某些情况下是),那么peek()肯定不仅仅是用于调试的目的,就像forEach()不仅仅是用于调试的目的一样。

对于我来说,当在forEach()和peek()之间进行选择时,是选择以下内容:我想要突变流对象(或改变全局状态)的代码片段附加到可组合的,还是我想要它们直接附加到流?

我认为peek()将更好地与java9方法配对。例如,takeWhile()可能需要根据一个已经发生变化的对象决定何时停止迭代,因此与forEach()匹配将不会产生相同的效果。

注:我没有在任何地方提到map(),因为如果我们想要突变对象(或全局状态),而不是生成新对象,它的工作方式与peek()完全相同。

关键是:

不要以一种意想不到的方式使用API,即使它实现了您的直接目标。这种方法在将来可能会失效,而且对于未来的维护者来说也不清楚。


将其分解为多个操作并没有害处,因为它们是不同的操作。以一种不明确和意外的方式使用API是有害的,如果在未来的Java版本中修改了这种特定的行为,可能会产生后果。

在此操作中使用forEach将使维护者清楚地知道帐户的每个元素都有预期的副作用,并且您正在执行一些可能会改变它的操作。

它也更传统,因为peek是一个中间操作,在终端操作运行之前不会对整个集合进行操作,但forEach确实是一个终端操作。通过这种方式,您可以围绕代码的行为和流程进行强有力的论证,而不是询问peek在此上下文中的行为是否与forEach相同。

accounts.forEach(a -> a.login());
List<Account> loggedInAccounts = accounts.stream()
                                         .filter(Account::loggedIn)
                                         .collect(Collectors.toList());

很多答案都有很好的观点,尤其是Makoto(被接受的)的答案相当详细地描述了可能存在的问题。但没有人真正说明它是如何出错的:

[1]-> IntStream.range(1, 10).peek(System.out::println).count();
|  $6 ==> 9

没有输出。

[2]-> IntStream.range(1, 10).filter(i -> i%2==0).peek(System.out::println).count();
|  $9 ==> 4

输出数字2,4,6,8。

[3]-> IntStream.range(1, 10).filter(i -> i > 0).peek(System.out::println).count();
|  $12 ==> 9

输出数字1 ~ 9。

[4]-> IntStream.range(1, 10).map(i -> i * 2).peek(System.out::println).count();
|  $16 ==> 9

没有输出。

[5]-> Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9).peek(System.out::println).count();
|  $23 ==> 9

没有输出。

[6]-> Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9).stream().peek(System.out::println).count();
|  $25 ==> 9

没有输出。

[7]-> IntStream.range(1, 10).filter(i -> true).peek(System.out::println).count();
|  $30 ==> 9

输出数字1 ~ 9。

[1]-> List<Integer> list = new ArrayList<>();
|  list ==> []
[2]-> Stream.of(1, 5, 2, 7, 3, 9, 8, 4, 6).sorted().peek(list::add).count();
|  $7 ==> 9
[3]-> list
|  list ==> []

(你懂的。)

这些示例是在jshell (Java 15.0.2)中运行的,并模拟了转换数据的用例(取代System。Out::println by list::add例如,在一些回答中也做过),并返回添加了多少数据。目前观察到的情况是,任何可以过滤元素的操作(如过滤或跳过)似乎都强制处理所有剩余的元素,但不需要保持这种方式。

也许经验法则应该是,如果您确实在“调试”场景之外使用peek,那么只有在确定终止和中间过滤条件是什么时才应该这样做。例如:

return list.stream().map(foo->foo.getBar())
                    .peek(bar->bar.publish("HELLO"))
                    .collect(Collectors.toList());

似乎是一个有效的情况,在一个操作中,将所有的foo转换为bar,并告诉他们都是你好。

似乎比下面的东西更高效和优雅:

List<Bar> bars = list.stream().map(foo->foo.getBar()).collect(Collectors.toList());
bars.forEach(bar->bar.publish("HELLO"));
return bars;

而且你最终不会迭代一个集合两次。