1. 程式人生 > 實用技巧 >Java8中findAny和findFirst的區別

Java8中findAny和findFirst的區別

試驗了一下java stream中的findAny和findFirst,發現都返回了列表中的第一個元素。那麼,這兩種方法的區別是什麼呢?

查看了一下Java API document:

findFirst:

findFirst

Optional<T>findFirst()

Returns anOptionaldescribing the first element of this stream, or an emptyOptionalif the stream is empty. If the stream has no encounter order, then any element may be returned.

This is ashort-circuiting terminal operation.

Returns:

anOptionaldescribing the first element of this stream, or an emptyOptionalif the stream is empty

Throws:

NullPointerException- if the element selected is null

顧名思義,即返回列表中的第一個元素。

這裡的short-circuiting是指:有時候需要在遍歷中途停止操作,比如查詢第一個滿足條件的元素或者limit操作。在Stream中short-circuiting操作有:anyMatch、allMatch、noneMatch、findFirst、findAny、limit,這些操作在Sink中都有一個變數來判斷是否短路,比如limit用的是m,match用的是stop,find用的是hasValue。

這裡的terminal operation是指:一個終結操作,比如foreach,IntStream.sum

那麼,findAny是什麼呢?

findAny

Optional<T>findAny()

Returns anOptionaldescribing some element of the stream, or an emptyOptionalif the stream is empty.

This is ashort-circuiting terminal operation.

The behavior of this operation is explicitly nondeterministic(不確定的); it is free to select any element in the stream. This is to allow for maximal performance in parallel operations; the cost is that multiple invocations(呼叫)on the same source may not return the same result. (If a stable result is desired, use

findFirst()instead.)

Returns:

anOptionaldescribing some element of this stream, or an emptyOptionalif the stream is empty

Throws:

NullPointerException- if the element selected is null

See Also:

findFirst()

可以看到findAny()操作,返回的元素是不確定的,對於同一個列表多次呼叫findAny()有可能會返回不同的值。使用findAny()是為了更高效的效能。如果是資料較少,序列地情況下,一般會返回第一個結果,如果是並行的情況,那就不能確保是第一個。比如下面的例子會隨機地返回OptionalInt[50]。

System.out.println(IntStream.range(0, 100).parallel().findAny());

讓我們來舉另外一個例子:

  1. List<String> lst1 = Arrays.asList("Jhonny", "David", "Jack", "Duke", "Jill","Dany","Julia","Jenish","Divya");
  2. List<String> lst2 = Arrays.asList("Jhonny", "David", "Jack", "Duke", "Jill","Dany","Julia","Jenish","Divya");
  3. Optional<String> findFirst = lst1.parallelStream().filter(s -> s.startsWith("D")).findFirst();
  4. Optional<String> fidnAny = lst2.parallelStream().filter(s -> s.startsWith("J")).findAny();
  5. System.out.println(findFirst.get()); //總是打印出David
  6. System.out.println(fidnAny.get()); //會隨機地打印出Jack/Jill/Julia