Java8 new features 3 Stream

https://blog.csdn.net/y_k_y/article/details/84633001

 

(1) concept

Stream processing is a key abstractions Java8 collection, you can specify that you want it to be a set of operations can be performed very complex search, filtering and mapping data and other operations. Use Stream API to manipulate the data collection, similar to using a SQL database query execution. Stream API may be used to perform operations in parallel. Briefly, Stream API provides an efficient and easy to use way of processing data.

Features:

        1 is not a data structure, the data will not be saved.

        2. does not modify the original data source will generate a new stream .

        3. The lazy evaluation, the intermediate process stream, only the operation will be recorded, and is not performed immediately, the time required until the termination operation is performed before the actual calculations.

 

 

Data collection is concerned, the stream of interest is calculated. 

(2) Operation

  1. Creating Stream: a data source (collections, arrays) to obtain a stream.
  2. Intermediate operation: a data source for the data processing.
  3. Terminating operation: produce a result.
package Stream;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

/**
 * @autour zdc
 * @create 2020-03-20-22:45
 */
public class _1Test {
    private List<Person> people = Arrays.asList(
            new Person("zd2",11,22.3), new Person("zd2",11,22.3),
            new Person("zdi",12,11d),new Person("zdi",12,11d),
            new Person("zdt",113,222.3),
            new Person("zdf",114,22.33),
            new Person("zdf",117,22.3),
            new Person("zddsd",8,22.3),
            new Person("zdcfff",1100,22.3)
    );

    @Test
    public void test1(){
        //1.通过Collection提供的stream()或parrallelStream()方法
        List<String> list = new ArrayList<>();
        Stream<String> stream = list.stream();

        //2.通过Arrays的stream()
    } / * 
    * Intermediate operation: a plurality of intermediate operations may be connected into a pipeline, performs all called lazy evaluation disposable in operation is terminated. 
    * Filter filtration flow (receiving the Predicate) 
    * flow cutoff limit 
    * distinct repetitive elements deleted (Comparative hashcode equals)

        

        

        


     
    * skip (n) prior to skip several elements, if the stream is less than n number of elements, an empty stream 
    * 
    * mapping: 
    * MAP-function receives as a parameter a (receiving function), the function will be applied to each element, and maps it into a new element 
    * flapMap- receiving as a function of a parameter value of each stream are replaced with another stream, then all flows connected to a stream. 
    * FlapMap receiving Function, function must return a Stream object! ! ! ! ! 
    * 
    * Sort by: 
    * sorted () Comparable a class implements the Comparable interface, the natural ordering 
    * sorted (Comparator comparator) Comparator custom sorting 
    * 
    * termination of operation: 
    *    
    * * / 

    @Test 
    public  void test2 () { 
        List <the Person> = List new new the ArrayList <the Person> ();
         //forEach intermediate filter operation to terminate the operation
        .. people.stream () filter ((X) -> x.getAge () <100) .skip (. 1) .distinct () forEach (p-> List.add (P)); 
        System.out.println ( List); 

        // map 
        people.stream () Map ((Person ->. { 
            person.setName (person.getName () + "Hitler" );
             return Person; 
        })) forEach (the System.out :: the println). ; 


        Stream <Stream <= people.stream character Stream >> () the Map (the person ->. {
             // the name of each person in each character taken out as a List -> Map <Stream <character >)> 
            List <Character> = List1 new new the ArrayList <> ();
             for(. Character Character: person.getName () toCharArray ()) { 
                list1.add (Character); 
            } 
            return list1.stream (); 
        }); 

//         stream.forEach (SM -> {
 //             sm.forEach (the System :: the println .out);
 //         }); 


        // flapMap- receiving a function as a parameter, the stream object must return the value of each stream are replaced with another stream, then all flows connected to a stream 
        . stream <Character> characterStream = people.stream () flatMap (Person -> { 
            List <Character> = List1 new new the ArrayList <> ();
             for . (Character Character: person.getName () toCharArray ()) { 
                List1. add (character);
            }
            return list1.stream();
        });
        characterStream.forEach(System.out::println);

        people.stream().sorted(Comparator.comparing(Person::getAge)).forEach(System.out::println);


    }
}

 

Guess you like

Origin www.cnblogs.com/zdcsmart/p/12535954.html
Recommended