Java 8 (4) stream flow-using

Source: Internet
Author: User
Tags stream api

Many of the operations supported by the stream API are described in this section, which can accomplish more complex data queries such as filtering, slicing, mapping, finding, matching, and attribution. There are also special flows such as numerical flows, streams from multiple sources such as files and arrays.

Filtering and slicing

1. Filtering with predicates

The streams interface supports the filter method, which takes a predicate as an argument and returns a stream that contains all the elements that conform to the predicate. For example, filter out all vegetarian dishes:

list<dish> Vegetarianmenu = Menu.stream (). Filter (Dish::isvegetarian). Collect (ToList ());

2. Filter the different elements

The stream sea supports a method called DISTINCT, which returns a stream of different elements (based on the hashcode of the elements generated by the stream and the implementation of the Equals method). For example, filter all the even numbers and make sure that there are no duplicates:

        List<integer> nums = arrays.aslist (1,2,3,13,12,2,1,2,2,1,2,2,3,4,5);        List<Integer> oddnums = Nums.stream (). Filter (s->s%2==0). Distinct (). Collect (ToList ());

3. Cutoff

The stream supports the limit (n) method, which returns a stream that does not exceed a given length, and the required length is passed as a parameter to the limit, and if the stream is ordered, it returns up to the first n elements. For example, the first 3 courses that filter calories over 300 calories:

list<dish> limit3 = Menu.stream (). Filter (C->c.getcalories () >300). Distinct (). Limit (3). Collect (ToList () );

4. Skipping elements

The stream also supports the Skip (n) method, which returns a stream that discards the first n elements and returns an empty stream if the element u in the stream is less than N. For example: Skip the first two courses over 300 calories and return to the rest.

List<dish> skip2 = Menu.stream (). Filter (C->c.getcalories () >300). Distinct (). Skip (2). Collect (ToList ());

Mapping

In SQL, for example, you can choose to select a column from a table, and the Stream API provides similar tools through the map and Flatmap methods.

  1. Apply a function to each element in the convection

The stream supports the map method, which takes a function as a parameter. This function is applied to each element and is mapped to a new element. For example, the following passes the Dish::getname to the map method to extract the name of the dish in the stream:

list<string> names = Menu.stream (). Map (Dish::getname). Collect (ToList ());

Because GetName returns a String, the type of stream that the map method outputs is stream<string>. For example, the following map list<string> to a list<integer> value is the length of a String.

List<string> STRs = arrays.aslist ("Lambda", "action", "Java 8", "Stream"); List<Integer> ints = Strs.stream (). Map (String::length). Collect (ToList ());

If you want to find out how long each dish's name is, you can add a map:

list<integer> namelength = Menu.stream (). Map (Dish::getname). Map (String::length). Collect (ToList ());

  2. Flattening of the stream

    

Java 8 (4) stream flow-using

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.