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

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);
主站蜘蛛池模板: 阳泉市| 年辖:市辖区| 宣城市| 莒南县| 鄂伦春自治旗| 博客| 宁海县| 康乐县| 肃宁县| 桐庐县| 天长市| 内江市| 乐至县| 石嘴山市| 巢湖市| 庆安县| 毕节市| 保山市| 陕西省| 洛川县| 内丘县| 伽师县| 柏乡县| 鄂尔多斯市| 嵊泗县| 余江县| 蕉岭县| 收藏| 盱眙县| 兴城市| 西林县| 咸丰县| 瑞安市| 肃北| 屏东市| 灵寿县| 延寿县| 延川县| 建湖县| 金堂县| 三都|