JAVA- base (Stream stream)

JAVA- base (Stream stream)

Speaking of everyone's first reaction stream flow is io, but who in fact is the presence of certain provisions stream io bag it? Thanks to the Lambda expressions in java8 functional programming, introduced a new concept, stream.

1. Advantages?

Before we walk through the collection java8 probably enhanced for loop, if we want to add some to add this collection to search for something in the collection, we will find that the code will become very messy, but we are now using stream will be very elegant solution this problem.

Code:

  1. First screened all people surnamed Mao;

  2. Then there are the names of people screened three words;

  3. Finally, the results printout.

. 1  Package cn.fan;
 2  . 3 Import of java.util.ArrayList;
 . 4 Import java.util.List;
 . 5 . 6 public class Stream1 {
 . 7 public static void main (String [] args) {
 . 8 // First Condition: filter name Mao
 9 // second condition: screening of words name
 10 // before printing . 11          List <String> List = new new the ArrayList <> ();
 12 is          List.add ( "Mao meow" );
 13          list.add ( "Luo three guns" );
 14                                       
         list.add("阿龟哥");
15         list.add("范小饭");
16         List<String> maoList = new ArrayList<>();
17         for (String name : list) {
18             if (name.startsWith("茂")) {
19                 maoList.add(name);
20             }
21         }
22 23         List<String> shortList = new ArrayList<>();
24         for (String name : maoList) {
25             if (name.length() == 3) {
26                 shortList.add(name);
27             }
28         }
29 30         for (String name : shortList) {
31             System.out.println(name);
32         }
33 34     }
35 }
View Code

So now there is a more elegant approach:

 1 package cn.fan;
 2  3 import java.util.ArrayList;
 4 import java.util.List;
 5  6 public class Stream2 {
 7     public static void main(String[] args) {
 8         List<String> list = new ArrayList<>();
 9         list.add("茂小喵");
10         list.add("罗三炮");
11         list.add("阿龟哥");
12         list.add("范小饭");
13 14         list.stream()
15                 .filter(s->s.startsWith("茂"))
16                 .filter(s->s.length()==3)
17                 .forEach(System.out::println);
18     }
19 }
View Code

Stream (stream) is a queue element from the data source. Element is a particular type of object, a queue is formed. In Java Stream and does not store elements, but on-demand computing. Origin of the data source. It may be a collection, such as an array.

"Stream Flow" is actually a collection of elements of the model function, it is not set, the data structure is not, in itself does not store any element (or address value).

2. Two Ways stream of?

default Stream<E> stream()

static <T> Stream<T> of(T... values)

. 1  Package com.itheima.demo02.Stream;
 2  . 3 Import Classes in java.util *. ;
 . 4 Import java.util.stream.Stream;
 . 5 . 6 / * . 7     java.util.stream.Stream <T> is the new Java 8 Join the most common stream interface. (This is not a function interface)
 8     acquires a stream very simple, there are several common methods:
 . 9         - All Collection set can obtain the flow stream by the default method;
 10             default Stream <E> stream ()
 . 11         - static method stream interface can get an array of the corresponding stream.
12 is             static <T> Stream <T> of (T ... values)
 13 is             parameter is a variable parameter, then we can pass an array of
 14 * / 15    
         
 public  class Demo01GetStream {
 16      public  static  void main (String [] args) {
 . 17          // to convert a collection of Stream stream 
18 is          List <String> List = new new the ArrayList <> ();
 . 19          Stream <String> list.stream STREAM1 = ( ); the Set <String> SET = new new HashSet <> ();
 20 is      Stream <String> STREAM2 = set.stream ();
 21 is  22 is      the Map <String, String> Map = new new the HashMap <> ();
 23 is // Get key, stored in a collection set 24      set <String> = keySet     
map.keySet ();
 25      Stream <String> STREAM3 = keySet.stream ();
 26 is  27 // get the values, stored in a collection Collection 28      Collection <String> values = map.values ();
 29      Stream <String > STREAM4 = values.stream ();
 30 31 is // Get key-value pair (entrySet mapping between keys and values) 32      the Set <of Map.Entry <String, String entries It >> = EnumMap.entrySet ();
 33 is      Stream < of Map.Entry <String, String >> Stream5 = entries.stream ();
 34 is 35 // the array into a stream stream 36     
      
      
     Stream <Integer> = stream6 Stream.of (. 1, 2,. 3,. 4,. 5 );
 37 [      // variable parameters can be passed array 
38 is      Integer [] ARR = {1,2,3,4,5 };
 39      Stream <Integer> = stream7 Stream.of (ARR);
 40      String [] = {arr2 is "A", "BB", "CCC" };
 41 is      Stream <String> stream8 = Stream.of (arr2 is);
 42 is  }
 43 is }
View Code

3.stream flow can only be used once? why?

First of all we need to know, it belongs to the pipeline flow stream flow, can only be consumed once, when the first stream flow method call is completed, the data will flow a stream flow, so the first stream flow is no data, and closed.

4. The mapping converts a data stream to another stream flow stream?

If we need to map the flow of the elements into a stream, we can use the map method

Code:

. 1  Package cn.fan;
 2  . 3 Import java.util.stream.Stream;
 . 4 . 5 / * . 6     conventional method Stream stream _map: for type conversions
 7     if needed to map the flow element to another stream can use the map method.
 . 8     <R & lt> stream <R & lt> map (function <Super T, the extends R & lt??> Mapper);
 . 9     this interface requires a function function interface parameters, the current stream type T data may be R into another type of flow.
10     abstract method Function:
 . 11         R & lt Apply (T T);
 12 is * / 13 is public class Stream_map {
 14 public static void main (String [] args) {
 15 //   
        
                  Obtaining a stream of type String Stream 
16          Stream <String> = Stream.of Stream ( ". 1", "2", ". 3", ". 4" );
 . 17          // use the map method, the integer string type, the conversion (map) is an integer of type integer 
18 is          stream <integer> = stream.map STREAM2 ((String S) -> {
 . 19              return the Integer.parseInt (S);
 20 is          });
 21 is          // traversing Stream2 stream 
22 is          stream2.forEach ( I-> System.out.println (I));
 23 is      }
 24 }
View Code

5. The end of the method, a data count stream flow?

 1 package cn.fan;
 2  3 import java.util.stream.Stream;
 4  5  6 public class StreamCount {
 7     public static void main(String[] args) {
 8         Stream<String> original = Stream.of("张无忌", "张三丰", "周芷若");
 9         Stream<String> result = original.filter(s -> s.startsWith("张"));
10         System.out.println(result.count()); // 2
11     }
12 13 }
View Code

6. access the first few: limit ?

It is a long type parameter, if the length is greater than the current set of parameters it is truncated; otherwise, no operation is performed. Basic use:

 1 package cn.fan;
 2 import java.util.stream.Stream;
 3  4 public class StreamLimit {
 5     public static void main(String[] args) {
 6         Stream<String> original = Stream.of("张无忌", "张三丰", "周芷若");
 7         Stream<String> result = original.limit(2);
 8         System.out.println(result.count()); // 2
 9     }
10 }
View Code

7. Skip the first few: skip?

If you want to skip the first few elements, a method may be used to obtain a new skip stream after a taken:

Stream skip(long n);

If the flow is greater than the current length of n, n a skip before; otherwise 0 will be a length of blank flow.

 1 package cn.fan;
 2  3 import java.util.stream.Stream;
 4  5 public class StreamSkip {
 6     public static void main(String[] args) {
 7         Stream<String> original = Stream.of("张无忌", "张三丰", "周芷若");
 8         Stream<String> result = original.skip(2);
 9         System.out.println(result.count()); // 1
10     }
11 12 }
View Code

8. The composition: concat?

If there are two streams, want to merge into a stream, you can use Stream interface static method concat:

static Stream concat(Stream a, Stream b)

Note: This is a static method, and the method concat java.lang.String which is different.

 1 package cn.fan;
 2  3 import java.util.stream.Stream;
 4  5 public class StreamConcat {
 6     public static void main(String[] args) {
 7         Stream<String> streamA = Stream.of("张无忌");
 8         Stream<String> streamB = Stream.of("张翠山");
 9         Stream<String> result = Stream.concat(streamA, streamB);
10     }
11 }
View Code

 

 

Guess you like

Origin www.cnblogs.com/fan123yh/p/11115157.html