Java8 new features Stream Streaming thought (c)

Just start writing blog, there are also no place where a lot of guidance please big brother forum, thank you!

  • forEach () method
cn.com.cqucc.demo02.StreamMethods.Test02.StreamMethods Package; 

Import of java.util.ArrayList; 
Import java.util.List; 
Import java.util.stream.Stream; 

/ * 
 * forEach method of practicing 
 * void forEach (Consumer <? super T> perform operations action) for each element of this stream. 
 * Parameters: 
 * Consumer is a function interface, will each stream element to the function to handle; 
 * it is a function of consumer interface, since the interface is a function, it is possible to use Lambda expressions. 
 * Role: 
 * through the data. 
 * Note: 
 * It is the end of a method, then you can not use other methods in the Stream flow, otherwise it will throw an exception. 
 * * / 
Public class Test01StreamForeach { 

    public static void main (String [] args) {
        List <String> List = new new ArrayList <> (); 
        list.add ( "Joe Smith");
        list.add ( "John Doe"); 
        list.add ( "Wang Wu"); 
        list.add ( "Zhao six"); 
        list.add ( "Tianqi"); 
        list.add ( "bastard"); 
/ / 1, is converted to first list the stream stream 
        stream <String> stream list.stream = (); 
// 2, using the method of the stream forEach through the collection 
        stream.forEach (s -> System.out.print (s + "" )); // output: Zhangsanlisi Wang Wu Zhao Tianqi six bastard 
// 3, the stream will happen if you use this? 
        stream.filter (s -> s.startsWith ( "Zhang")); 
// this will throw an exception: a java.lang.IllegalStateException: Stream has already been operated upon or Closed 
    } 
}

  

  • filter () method

 

cn.com.cqucc.demo02.StreamMethods.Test02.StreamMethods Package; 

Import java.util.stream.Stream; 

/ * 
use * Stream the filter () method 
* Stream <T> filter (Predicate <? super T> predicate ) this return flow from the elements of a given predicate matching the composition of this stream. 
* Parameters: 
* receiving the process parameter is a function interface, it is possible to use Lambda expressions 
* Function: filtering elements 
* 
* Stream flow characteristics: 
            1, belonging to the flow conduit can be consumed (used) time 
            2 the first stream flow method call is completed the data will flow on to the next stream stream flow at this time had been used up first will be closed 
                at this time first stream flow can not call for class of 
                our stream flows only can be used once 
* * / 
public class Test02StreamFilter { 

    public static void main (String [] args) { 
// create a stream
        Stream <String> streamA = Stream.of ( " Joe Smith", "John Doe", "Wang five", "Zhao six", "Tianqi", "bastard"); 
// I just need surname "Chang" in so call stream flow filtration method, filter 
        streamA.filter (s -> s.startsWith ( "Zhang")) forEach (s -> System.out.println (s)); // output: Zhang 

@ Note 
        Stream <String> streamB = Stream.of ( " Bob", "Hua", "king"); 
        Stream <String> streamC streamB.filter = (S -> s.startswith ( "Lee")); 
/ / streamB.forEach (s-> System.out.println (s )); // this time before using streams will throw an exception IllegalStateException 
        streamC.forEach (S -> System.out.println (S)); / / output: Hua 
    } 
}

  

  • map () method
cn.com.cqucc.demo02.StreamMethods.Test02.StreamMethods Package; 

Import java.util.stream.Stream; 

/ * 
 * <R & lt> Stream <R & lt> Map (Function Mapper <R & lt Super T ,? the extends?>) Returns From the results given function applied to elements of this stream consisting of the stream. 
 * Parameters: 
 * a function interface Function, the transfer function T may be types of data stream type for the type R 
 * can convert one type to another type, called mapping. 
 * The role: 
 * map mapping: If you need to map elements to the stream in another stream, the method will use the map 
 * * / 
public class Test03StreamMap { 

    public static void main (String [] args) { 
// requirements: string integer integer integer type to a type of 
        Stream <String> stringstream = Stream.of ( ". 1", "2", ". 1", ". 3"); 
        Stream <integer> 
        integerStream.forEach (S -> System.out.println (S)); 
    } 
}

  

  • count () method
cn.com.cqucc.demo02.StreamMethods.Test02.StreamMethods Package; 

Import of java.util.ArrayList; 
Import java.util.List; 
Import java.util.stream.Stream; 

/ * 
 * Method Stream stream long count ( ) returns the number of elements in this stream. 
 * 
 * Function: 
 * Stream flow statistics for the number of elements 
 * Note: 
 * COUNT is a method of termination, then it can not be finished using other methods used in the stream Stream 
 * 
 * * / 
public class Test04StreamCount { 

    public static main void (String [] args) { 

        List <String> = new new List the ArrayList <> (); 
        List.add ( "John Doe"); 
        List.add ( "John Doe"); 
        List.add ( "Wang Wu" ); 
        list.add ( "Zhao six"); 
        List.
        list.add ( "bastard"); 

        Stream <String> streamList = list.stream (); 
// Long COUNT = streamList.count (); 
        Long COUNT = streamList.count (); // call after it no longer calls other methods of flow 
        System.out.println (COUNT); //. 6 
    } 
}

  

  • limit () method
cn.com.cqucc.demo02.StreamMethods.Test02.StreamMethods Package; 

Import of java.util.ArrayList; 
Import java.util.List; 
Import java.util.stream.Stream; 

/ * 
 * Stream methods Stream <T> limit (long maxSize) thereby return the element flow stream composition, the truncated length can not exceed maxSize. 
 * Parameters: 
 * length greater than a Parameter is a long type of data if the set is interception otherwise do nothing 
 * effect: 
 * limit method may be elements taken convection only taking the first n 
 * * / 
public class {Test06StreamLimit 

    public static void main (String [] args) { 
        // Get a stream stream 
        List <String> = new new List the ArrayList <> (); 
        List.add ( "John Doe"); 
        List.add ( "John Doe" ); 
        List.
        list.add ( "Zhao six"); 
        list.add ( "Tianqi"); 

        Stream stream list.stream = <String> (); 
// method using the limit stream flow interception elements, as long as the first two flow element and traversed 
        stream.limit (2) .forEach (S-> System.out.println (S)); 
    } 
}

  

  • skip () method
cn.com.cqucc.demo02.StreamMethods.Test02.StreamMethods Package; 

Import of java.util.ArrayList; 
Import java.util.List; 
Import java.util.stream.Stream; 

/ * 
 * Stream methods Stream <T> skip (Long n) after the first stream of n elements discarded, the remaining elements of the return flow from the flow thereof. 
 * Parameters: 
 * is a long type of data 
 * Function: 
 * If you want to skip the first n elements of the acquisition stream is taken after a skip method using the Stream 
 * if the current length of the stream is greater than n, n is skipped or ex- to a stream of length zero. 
 * * / 
Public class Test06StreamSkip { 

    public static void main (String [] args) { 
        List <String> = new new List the ArrayList <> (); 
        List.add ( "John Doe"); 
        List.add ( "ZHAO Si") ; 
        list.add ( "King of five");
        list.add ( "Zhao six"); 
        list.add ( "Tianqi"); 
        list.add ( "bastard"); 

        Stream <String> Stream list.stream = (); 
        stream.skip (. 3) .forEach ( s -> System.out.print (s + " ")); // output: Zhao six pseudo-ginseng bastard 
    } 
}

  

  • concat method
cn.com.cqucc.demo02.StreamMethods.Test02.StreamMethods Package; 

Import of java.util.ArrayList; 
Import java.util.List; 
Import java.util.stream.Stream; 

/ * 
 * Stream interface static <T> Stream <T> the concat (stream <? the extends T> a, stream <? the extends T> B) 
 * create a lazy stream connection, all of whose elements are the first elementary stream, followed by all the second elementary stream. 
 * Parameters: 
 * two streams 
 * Role: 
 * If you have two streams, you want to merge into one stream, then you can use the static method Stream flow concat 
 * * / 
public class Test07StreamConcat { 

    public static void main (String [] args) { 

        List <String> = new new listA the ArrayList <> (); 
        listA.add ( "John Doe"); 
        listA.add ( "John Doe"); 
        listA.
        listA.add ( "Zhao six"); 
        listA.add ( "Tianqi"); 
        listA.add ( "bastard"); 

        List <String> = new new listB the ArrayList <> (); 
        listB.add ( "Bob") ; 
        listB.add ( "Zhang Qiang"); 
        listB.add ( "Hua"); 

        Stream streamA = listA.stream <String> (); 
        Stream streamB = listB.stream <String> (); 

        Stream <String> = concatAB Stream.concat (streamA, streamB); 

        concatAB.forEach (S -> of System.out.print (S + "")); // Zhangsanlisi six TianQi Zhao Wang Wu Qiang Hua Xiaoming bastard 
    } 
}

  

Original: https: //blog.csdn.net/qq_41319058/article/details/90321251

Guess you like

Origin www.cnblogs.com/qbdj/p/10951090.html