Javase for busy People 8-learning

Source: Internet
Author: User

The lambda and function interfaces are mentioned earlier, but Javase 8 provides a lot of useful things in addition to the two new features. For example, stream.

Groping for a few days, finally figured out the application of stream.

Recommend a post: Streams API in Java 8

The stream here is the API for manipulating the collection. A bit like assembly line work, can be a line down, can also be divided into multiple lines parallel.

Stream is not a data structure, but an algorithm and computation. So the stream only needs to be told what to do without being told what to do.

For example, a collection A has a lot of elements, and now you want to manipulate those elements.

Traditional way: An iterator or a for loop traversal, and then operate on each iteration or traversed element, in a sense, this is an orderly execution.

Stream: You just need to pass in the action you want to take on the element (lambda).

    "" need to tell it what to do, don't need to tell it what to do. For example, statistics need not be ordered.
     Step: ① Create a Stream;② in one or more steps, you specify an intermediate operation that converts the initial stream to another stream; ③ uses a terminating operation to produce a result. The stream will not be available after that.
   The new method in the  collection interface, you can turn any object into a stream. If it is an array, you can use Stream.of () to turn to stream.
    
     and:stream<stream<object>>.
    
     If you want to perform an action on each object in the stream, use Stream.map (lambda);
     "" However, if stream is nested within a stream, you need to use "stream.flatmap (lambda);". In this case, you need to invoke the stream method in the lambda operation. The
         "" "" " Flatmap method is similar to the map method, except that the return value of the mapping function is different. The mapping function return value of the map method can be any type T, and the mapping function of the Flatmap method must be stream.
    

The limit () method of the

stream, which can intercept the child stream. Can be used with stream.generate (lambda). limit (100);
    
    stream aggregation operations, such as Count (), Max (), Min (), and so on, can be understood by reference to the aggregate function of the database.
     "" "" to note that these aggregate functions return the optional object, which effectively avoids the null condition--JAVA8 the recommended action.     
    
    
         Stream.reduce (), the common method is average, sum, Min, Max, and Count, which returns a single result value, and the reduce operation always creates a new value for each element processed.
            //reduce () receives a starting value (seed), then the seed and the subsequent action, and the result again as a seed. --the background method will definitely choose a seed (either manually declare or let the background Select)
            //Manual declaration: Stream.reduce ( Seed, Lambda)   The lambda here only needs to declare one parameter, and then can manipulate both the seed and the parameter and return the result.
            //Background selection: Stream.reduce (lambda)    The lambda here requires an explicit declaration of two parameters. The background fills in the elements of the stream.


Stream.collect Unlike the Stream.reduce method, Stream.collect modifies an existing value instead of creating a new value for each element that is processed.
Collect needs to receive three parameters (what type of object to save to, which method to receive with that type, how many objects of that type are federated-parallel)
Note that the type returned by ①stream.collect is the type to which it is saved. A method with three parameters (multiple methods) has been provided in the ②collectors.

Custom Stream:Stream.generate () and Stream.iterate ().
Stream.generate (Supplier).
Stream.iterate (seed, F) is much like the reduce operation, accepting a seed value, and a unaryoperator (for example, f). The seed value then becomes the first element of the Stream, F (Seed) is the second, F (Seed) is the third, and so on.

"" "If you turn from array to stream, the stream is ordered, but can be turned into disorder.

"" "" "" "" Simply put, the use of Stream is to implement a filter-map-reduce process, produce a final result, or cause a side effect (side effect).

Javase for busy People 8-learning

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.