Java8 new set of actions Stream

1.java8 added Stream, IntStream, LongStream the like, which is a generic stream Stream interface also provides a corresponding flow for each of Api Builder, e.g. Stream.Builder, developers can create these corresponding Builder flow. you might want to ask what is the stream?

2. What is Stream
Stream is not a collection of elements, it is not a data structure of the data is not saved , it is about algorithms and calculations, it is more like an advanced version of Iterator. The original version of the Iterator, a user can explicitly through the elements and perform a certain operation; Advanced versions Stream, as long as the user needs to perform any given operation of its contained elements, such as "filter out length greater than 10 string "," Get the first letter of each string "and, implicitly Stream traversed internally converting the data accordingly. 
Stream like an iterator (the Iterator), unidirectional, not reciprocating, data can only be traversed once traversed once after exhausted, like water flows from the front, gone. 
And while the iterator is different and, in parallel Stream operation, only imperative iterators, the serialization operation. As the name suggests, when a serial mode to traverse, and then read each item to read the next item. When used to traverse the parallel data is divided into a plurality of segments, each of which is processed in a different thread, and then outputs the results together. Fork Stream dependent parallelism introduced in Java7 / Join frame (JSR166y) to accelerate the process and split task.

The main difference between Stream and Collection has: 
1.stream itself does not store data, the data is stored in the corresponding collection in, or generated only when needed; 
2.stream does not modify the data source, always returns a new stream ; 
3.stream lazy operation is performed (the lazy): the final result only when the required time will be implemented, such as the above example, the results of the former requires only three strings of length greater than 7, and then locate the first 3 after the string length to meet the requirements, filter () to stop executing;

 

3. Stream independently of the following steps:

1. Stream or XxxStream the Builder () method creates a modified class corresponding to Stream Builder.

2. Repeat the Builder calling add () method to add elements to the plurality of streams.

3. Call Builder's build () method to get the corresponding Stream.

4. Call Stream aggregation method.

Step 4 can be invoked in different ways according to the needs, Stream provides a method for users to gather a large number of calls, correspondence for most aggregation processes, each Stream can only be performed once.

Simply put, it is three steps:

1. Create stream; 
2. through one or more intermediate operations (intermediate operations) the initial stream into another stream; 
3. obtaining results abort operation (terminal operation); the operation is performed before the operation trigger lazy, suspension after the operation, the stream is closed, it can not be used;

public class demo2 {
	public static void main(String[] args) {
         Builder bd = IntStream.builder().add(15).add(13).add(-9).add(38);         
         IntStream stream = bd.build();  
      System.out.println("stream获取的最大值:"+ stream.max().getAsInt());
      
      System.out.println("stream获取的最小值:"+ stream.min().getAsInt());

We printed above a maximum and a minimum. Then look at the print station

Found being given, also confirmed the above statement, the operation is aborted, the stream is closed, no longer be used;

But also in the collection of Stream interface, by a method to rewrite Stream set of conditions we need; for example: "Return the number of elements eligible"

public static void main(String[] args) {
        Collection<String> books = new HashSet<>();
        books.add("abcdefg");
        books.add("c3p0");
        books.add("1236789");
       Stream<String> str = books.stream();
        System.out.println(str.filter(ele -> ((String) ele).contains("678")).count());

Method of operation, see the results print out a print station we want:

So, we split up the old look, along with a step by step look at

  /**
         * 拆开一步步分析来看
         */
        Stream<String> stream = str.filter(ele -> ((String) ele).contains("123"));       
        long count = stream.count();        
        System.out.println(count);

1. First, we now go str.filte see the following:

Stream is a return type, the parameter is a filter;

About filtering criteria ele -> ((String) ele) .contains ( "678")) count (), we can do that:

boolean testFunction(String element) 
{
    if (element.contains("678")))
        return true;
    else
        return false;
}

It is about a collection of all the conditions included 678 filter out. Modern computer languages ​​tend to optimizing compilers and operating speed takes the form of a lambda expression is equivalent to calling the function does not require a named.

Then we point stream.count (); found as follows:

It returns a type long.

 

Stream conversion

Construction of a new Stream

public class demo2 {
	public static void main(String[] args) {
         Builder bd = IntStream.builder().add(15).add(13).add(-9).add(38);         
         IntStream stream = bd.build();  
         //将stream映射成一个新的Stream,新Stream的每个元素是原Stream元素的5倍+2
          IntStream newIs = stream.map(ele -> ele * 5 +2);
          //使用方法引用的方式来遍历集合元素
          newIs.forEach(System.out::println);
          //或者使用lambad來遍歷
          //newIs.forEach(str -> System.out.println(str));

Look out:

About stream there are many, you can go take a look at the api

 

Please indicate the source: https://blog.csdn.net/FindHuni/article/details/84940311

 

 

Guess you like

Origin blog.csdn.net/FindHuni/article/details/84940311