java8-9-Stream 的中间操作

 
 
Stream 的中间操作
 
                                    filter 过滤 排除元素
 
 
                    limit 截断 取得多少个结果
 
 
                skip 跳过几个结果  数量不足 返回空
 
 
                    distinct 去重复
 
 
                    map 根据Lambda体中的操作 提取元素到 流中
                             若Lambdda体操作返回Stream()流
                             结果会是两层流    需要遍历两次
 
flapMap  平铺map  将元素提取到一个流中
 
1.自然排序  java.lang.Comparable
 
 
2.自然排序 java.util.Comparator
 
 
 
 
 
  1 package com.wf.zhang.java8.stream;
  2 
  3 import com.wf.zhang.java8.lamdba.Employee;
  4 import org.junit.Test;
  5 
  6 import java.util.ArrayList;
  7 import java.util.Arrays;
  8 import java.util.List;
  9 import java.util.Objects;
 10 import java.util.stream.Stream;
 11 
 12 public class TestStream2 {
 13 
 14     //准备数据
 15     List<Employee> list = Arrays.asList(
 16             new Employee("张飞", 18, 5000.00),
 17             new Employee("赵云", 28, 6666.66),
 18             new Employee("关羽", 38, 7777.77),
 19             new Employee("曹阿蛮", 29, 7777.77),
 20             new Employee("刘备", 48, 10000.88),
 21             new Employee("刘备", 48, 10000.88),
 22             new Employee("刘备", 48, 10000.88),
 23             new Employee("刘备", 48, 10000.88),
 24             new Employee("诸葛亮", 26, 10000.88)
 25     );
 26 
 27 
 28     /**
 29      * stream 中间操作  筛选
 30      * <p>
 31      * filter  过滤   排除元素
 32      * limit   截断   取得几个结果
 33      * skip    跳过几个结果  若是数量不足 返回空
 34      * distinct  去重复 必须重写 hashCode() 和 equals()
 35      */
 36     @Test
 37     public void test01() {
 38         list.stream().filter((t) -> t.getSalary() > 5000)
 39                 .forEach(System.out::println);
 40     }
 41 
 42     @Test
 43     public void test02() {
 44         list.stream().filter((t) -> t.getAge() > 20)
 45                 .limit(1)
 46                 .forEach(System.out::println);
 47     }
 48 
 49     @Test
 50     public void test03() {
 51         list.stream().filter((t) -> t.getSalary() > 5000)
 52                 .skip(2)
 53                 .forEach(System.out::println);
 54     }
 55 
 56     @Test
 57     public void test04() {
 58         list.stream().filter((t) -> t.getSalary() > 5000)
 59                 .skip(2)
 60                 .distinct()
 61                 .forEach(System.out::println);
 62     }
 63 
 64     /**
 65      * stream 中间操作  映射
 66      * <p>
 67      * map 根据Lambda体中的操作  提取元素  到流中
 68      * 若Lambda体操作中返回stream()流  会是两层stream()流
 69      * 需要遍历两次
 70      * <p>
 71      * flatMap 将元素 添加到 新的一个流中
 72      */
 73     @Test
 74     public void test05() {
 75 
 76         List<String> stringList = Arrays.asList("aaa", "bbb", "ccc");
 77         stringList.stream()
 78                 .map((s) -> s.toUpperCase())
 79                 .forEach(System.out::println);
 80 
 81         System.out.println("----------------------------");
 82 
 83         //Employee的集合按照名字提取出来
 84         list.stream()
 85                 .map(Employee::getName)
 86                 .forEach(System.out::println);
 87 
 88 
 89         System.out.println("----------------------------");
 90 
 91 
 92         //将stringList 调用 filterchar方法
 93         Stream<Stream<Character>> stream = stringList.stream()
 94                 .map(TestStream2::filterchar);
 95         //map
 96         // 两层 stream 流遍历
 97         stream.forEach((sm) -> {
 98             sm.forEach(System.out::println);
 99         });
100 
101         System.out.println("+++++++++++++++++++++++++++++++++++++");
102 
103 
104         Stream<Character> characterStream = stringList.stream()
105                 .flatMap(TestStream2::filterchar);
106         //flatMap 一层遍历
107         characterStream.forEach(System.out::println);
108     }
109 
110     /**
111      * 定义一个方法 将字符串转成字符数组 提取一个个字符的 Stream流
112      *
113      * @param str
114      * @return
115      */
116     public static Stream<Character> filterchar(String str) {
117         List<Character> charlist = new ArrayList<>();
118         for (Character ch : str.toCharArray()) {
119             charlist.add(ch);
120         }
121         return charlist.stream();
122     }
123 
124     /***
125      * stream 中间操作   排序
126 
127      *                         1 自然排序:java.lang.Comparable  Comparable接口   compareTo()方法
128      *
129      *                        2 定制排序:java.util.Compartor   Comparator接口   compare()方法
130      */
131     @Test
132     public void test0e6() {
133 
134         List<String> stringList = Arrays.asList("fff","aaa", "bbb", "ccc");
135         //1 自然排序
136                  stringList.stream()
137                            .sorted()
138                            .forEach(System.out::println);
139 
140         System.out.println("---------------------------------");
141 
142         //2 定制排序
143                  list.stream()
144                      .sorted((x, y) ->{
145                          if (Objects.equals(x.getAge(),y.getAge())) {
146                              return x.getName().compareTo(y.getName());
147                          }else  {
148                                return Integer.compare(x.getAge(),y.getAge());
149                          }
150                      }).forEach(System.out::println);
151     }
152 
153 
154 }
View Code

猜你喜欢

转载自www.cnblogs.com/wf-zhang/p/11829100.html