官术网_书友最值得收藏!

Finding elements

Finding elements in Java streams has the following variants:

  • Find any element of the stream: The method findAny() is a short circuited terminal operation where the terminal operation returns the first encountered value of the stream before exiting the operation. In the case of an empty stream an empty optional is returned. It is also a non-deterministic function and hence the execution in parallel may return different values in each execution:
System.out.println("In a paralled stream from 5-100 finding any element :: "+IntStream.range(5, 100).parallel().findAny());
  • Find first elements of the stream: This is a deterministic function and it returns the first element of the stream or an empty optional in the case of an empty stream:
System.out.println("In a paralled stream from 8-100 finding the first element :: "+IntStream.range(8, 100).parallel().findFirst());
  • Count: This counts the number of elements present in a stream and then returns its value as a long:
Long elementCount=streamSupplier.get().count();
System.out.println("The number of elements in the stream are:: "+elementCount);
主站蜘蛛池模板: 沁阳市| 章丘市| 高尔夫| 盐津县| 张北县| 江川县| 恩平市| 定安县| 五莲县| 苏州市| 鱼台县| 扎兰屯市| 许昌市| 额敏县| 宝应县| 大安市| 万安县| 石景山区| 荆门市| 台南市| 红安县| 千阳县| 鹤岗市| 柏乡县| 嘉义县| 盐山县| 四平市| 荆州市| 龙陵县| 泸水县| 陵川县| 东辽县| 沽源县| 寿阳县| 乌兰浩特市| 怀集县| 平原县| 漯河市| 黄冈市| 遂平县| 泗水县|