Basic use of Java 8 stream stream __java

Source: Internet
Author: User
Tags java 8 stream
Use of data flow (stream)
1) Stream's acquisition

① through the collection collection get

1
list<integer> list = new Arraylist<integer> (arrays.aslist (1,2,3,4,5));
2
stream<integer> stream = List.stream ();

② is obtained by array

1
String[] Array = {"Are", "You", "OK"};
2
stream<string> stream = Array.stream (Array);
3
4
Handling of primitive type arrays
5
Int[] array = {1,2,3,4,5};
6
stream<integer> stream = Arrays.stream (array). boxed (); At this point Arrays.stream (array) gets a Intstream object

③ by value (as far as possible using the above 2 ways to get, this way there are defects)

1
stream<string> stream = Stream.of ("Are", "You", "OK");


2) Filtering filter

The filter function receives a lambda expression as a parameter that returns a Boolean, in which the stream transports the element to filter one at a time and filters out the element that executes the result to true;

1
Filter out non-empty items in a list
2
list<string> list = Arrays.aslist ("Are", "You", "", "OK");
3
list<string> filted = List.stream ()
4
  . Filter (x->!x.empty ())
5
  . Collect (Collectors.tolist ());
3) Go to heavy distinct
1
list<string> list = Arrays.aslist ("Are", "You", "You", "OK");
2
list<string> distincted = List.stream ()
3
  . Distinct ()
4
  . Collect (Collectors.tolist ());
4) Interception of limit

The first n elements of an intercept stream:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.