Stream之filter、distinct、skip、map、flatMap、match、find、reduce

A, Stream of filter, distinct, skip:

 1 package com.cy.java8;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 import java.util.stream.Collectors;
 6 
 7 public class StreamFilter {
 8 
 9     public static void main(String[] args) {
10         List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 6, 7, 7, 8);
11 
12         //取出偶数
13         List<Integer> result = list.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
14         System.out.println (Result);
 15  
16          // deduplication 
. 17          List <Integer> = RESULT1 list.stream () DISTINCT () the collect (Collectors.toList ());..
 18 is          System.out.println (RESULT1) ;
 . 19  
20 is          // skip over the five elements 
21 is          List <Integer> = result2 list.stream () skip (. 5. ) .collect (Collectors.toList ());
 22 is          System.out.println (result2);
 23 is  
24          // wherever the preceding 5 
25          List <Integer> = result3 list.stream () limit (. 5. ) .collect (Collectors.toList ());
 26 is          System.out.println (result3);
 27      }
28 }

Print Results:

[2, 4, 6, 6, 8]
[1, 2, 3, 4, 5, 6, 7, 8]
[6, 6, 7, 7, 8]
[1, 2, 3, 4, 5]

Two, Stream of the map, flatMap:  

 1 package com.cy.java8;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 import java.util.stream.Collectors;
 6 import java.util.stream.Stream;
 7 
 8 public class StreamMap {
 9 
10     public static void main(String[] args) {
11         List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 6, 7, 7, 8);
12 
13         //list集合中每个数放大两倍
14         List<Integer> result1 = list.stream().map(i -> i * 2) .collect (Collectors.toList ());
 15          System.out.println (RESULT1);
 16  
. 17          // returns only the Dish name 
18 is          .. List <String> result2 = listDish () Stream () Map (D - > d.getName ()) the collect (Collectors.toList ());.
 . 19          System.out.println (result2);
 20 is  
21 is          / ** 
22 is           * demand: the Hello World word and remove duplicate letters
 23 is           * Flat flatMap (flat)
 24           * / 
25          String [] = {words "the Hello", "World" };
 26 is          // {H, E, L, L, O}, {W is, O, R & lt, L, D} 
27          Stream <String []> stream =    Arrays.stream (words) .map (w -> w.split ( ""));//Stream<String[]>
28         //H,e,l,l,o,W,o,r,l,d
29         Stream<String> stringStream = stream.flatMap(Arrays::stream);
30         List<String> result3 = stringStream.distinct().collect(Collectors.toList());
31         System.out.println(result3);
32     }
33 
34     private static List<Dish> listDish(){
35         List<Dish> menu = Arrays.asList(
36                 new Dish("pork", false, 800, Dish.Type.MEAT),
37                 new Dish("beef", false, 700, Dish.Type.MEAT),
38                 new Dish("chicken", false, 400, Dish.Type.MEAT),
39                 new Dish("french fries", true, 530, Dish.Type.OTHER),
40                 new Dish("rice", true, 350, Dish.Type.OTHER),
41                 new Dish("season fruit", true, 120, Dish.Type.OTHER),
42                 new Dish("pizza", true, 550, Dish.Type.OTHER),
43                 new Dish("prawns", false, 300, Dish.Type.FISH),
44                 new Dish("salmon", false, 450, Dish.Type.FISH));
45         return menu;
46     }
47 }

Print Results:

[2, 4, 6, 8, 10, 12, 12, 14, 14, 16]
[pork, beef, chicken, french fries, rice, season fruit, pizza, prawns, salmon]
[H, e, l, o, W, r, d]

 

Three, stream of the match, find, reduce:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

-----

 

Guess you like

Origin www.cnblogs.com/tenWood/p/11517715.html