Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Stream in java and what to pay attention to

Shulou Source: shulou.com Published: 2022-05-31 10:48:21 10月03日 Update

In this article, the editor introduces in detail "how to use Stream in java and what to pay attention to", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use Stream in java and what to pay attention to" can help you solve your doubts.

1. Usage process, instantiation of Stream, a series of intermediate operations,. Terminate the operation

2. Pay attention to the intermediate operation chain to deal with the data of the data source. Once the termination operation is performed, the intermediate operation chain is executed and the result is produced. After that, it will no longer be used.

Example

Public class StreamAPITest1 {/ / create Stream method 1: through the collection @ Test public void test1 () {List employees = EmployeeData.getEmployees (); / / efault Stream stream (): return a sequential stream Stream stream = employees.stream (); / / default Stream parallelStream (): return a parallel stream Stream employeeStream = employees.parallelStream () } / / create Stream method 2: through the array @ Test public void test2 () {int [] arrs = {1,2,3,6,2}; / / call static Stream stream of Arrays class (T [] array): return a stream IntStream stream = Arrays.stream (arrs); Employee E1 = new Employee (1001, "Tom"); Employee e2 = new Employee (1002, "Jerry") Employee [] employees = {E1, e2}; Stream stream1 = Arrays.stream (employees);} / / create Stream method 3: through Stream's of () @ Test public void test3 () {Stream integerStream = Stream.of (12, 34, 45, 65, 76) } / / create Stream method 4: create stream @ Test public void test4 () {/ / iteration / / public static Stream iterate (final T seed, final UnaryOperator f) / / traverse the first 10 even Stream.iterate (0, t-> t + 2) .limit (10) .forEach (System.out::println) / / generate / / public static Stream generate (Supplier s) Stream.generate (Math::random) .limit (10) .forEach (System.out::println) }} here, the article "how to use Stream in java and what to pay attention to" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Tags: Methods articles content examples data processing proper even ideas data sources arrays new knowledge more steps processes knowledge knowledge points articles details results Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi NVidia MySQL Docker OPPO Reno