Java 8 Stream findFirst() vs. findAny() – Java 8 Stream findFirst() vs. findAny()

最后修改: 2017年 1月 22日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

The Java 8 Stream API introduced two methods that are often misunderstood: findAny() and findFirst().

Java 8 Stream API引入了两个经常被误解的方法。findAny()/em>和findFirst()/em>。

In this quick tutorial, we’ll look at the difference between these two methods and when to use them.

在这个快速教程中,我们将看看这两种方法的区别以及何时使用它们。

2. Using Stream.findAny()

2.使用 Stream.findAny()

As the name suggests, the findAny() method allows us to find any element from a Stream. We use it when we’re looking for an element without paying an attention to the encounter order:

顾名思义,findAny()方法允许我们从一个Stream中找到任何元素。当我们寻找一个元素时,我们会使用它,而不关注相遇的顺序。

The method returns an Optional instance, which is empty if the Stream is empty:

该方法返回一个Optional实例,如果Stream是空的,则该实例为空。

@Test
public void createStream_whenFindAnyResultIsPresent_thenCorrect() {
    List<String> list = Arrays.asList("A","B","C","D");

    Optional<String> result = list.stream().findAny();

    assertTrue(result.isPresent());
    assertThat(result.get(), anyOf(is("A"), is("B"), is("C"), is("D")));
}

In a non-parallel operation, it will most likely return the first element in the Stream, but there is no guarantee for this.

在非并行操作中,它很可能会返回Stream中的第一个元素,但并不保证这一点。

For maximum performance when processing the parallel operation, the result cannot be reliably determined:

在处理并行操作时,为了获得最大的性能,不能可靠地确定结果。

@Test
public void createParallelStream_whenFindAnyResultIsPresent_thenCorrect()() {
    List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
    Optional<Integer> result = list
      .stream().parallel()
      .filter(num -> num < 4).findAny();

    assertTrue(result.isPresent());
    assertThat(result.get(), anyOf(is(1), is(2), is(3)));
}

3. Using Stream.findFirst()

3.使用 Stream.findFirst()

The findFirst() method finds the first element in a Stream. So, we use this method when we specifically want the first element from a sequence.

findFirst()方法找到Stream中的第一个元素。因此,当我们特别想要一个序列中的第一个元素时,我们使用这个方法。

When there is no encounter order, it returns any element from the Stream. According to the java.util.streams package documentation, “Streams may or may not have a defined encounter order. It depends on the source and the intermediate operations.”

当没有相遇顺序时,它会返回Stream中的任何元素。根据 java.util.streams包的文档,”流可能有也可能没有定义的相遇顺序。这取决于源和中间操作”。

The return type is also an Optional instance, which is empty if the Stream is empty too:

返回类型也是一个Optional实例,如果Stream也是空的,它就是空的。

@Test
public void createStream_whenFindFirstResultIsPresent_thenCorrect() {

    List<String> list = Arrays.asList("A", "B", "C", "D");

    Optional<String> result = list.stream().findFirst();

    assertTrue(result.isPresent());
    assertThat(result.get(), is("A"));
}

The behavior of the findFirst method does not change in the parallel scenario. If the encounter order exists, it will always behave deterministically.

findFirst方法的行为在平行场景中不会改变。如果相遇的顺序存在,它的行为将总是确定性的。

4. Conclusion

4.结论

In this article, we looked at the findAny() and findFirst() methods of the Java 8 Streams API.

在这篇文章中,我们研究了Java 8 Streams API的findAny() findFirst()方法。

The findAny() method returns any element from a Stream, while the findFirst() method returns the first element in a Stream.

findAny()方法返回Stream中的任何元素,而findFirst()方法返回Stream的第一个元素。

The complete source code and all code snippets for this article are over on GitHub.

本文的完整源代码和所有代码片断都在GitHub上