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

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);
主站蜘蛛池模板: 伽师县| 玉门市| 宝丰县| 东丽区| 天祝| 湟源县| 册亨县| 大关县| 洞口县| 沅江市| 崇义县| 中超| 观塘区| 新龙县| 靖宇县| 恩施市| 五家渠市| 忻城县| 古田县| 紫云| 莱芜市| 莎车县| 新巴尔虎右旗| 景泰县| 望城县| 延庆县| 新民市| 自贡市| 满城县| 淅川县| 兴海县| 桦川县| 南丰县| 措美县| 涞源县| 麟游县| 乌拉特后旗| 宝兴县| 涟源市| 资中县| 阳西县|