|
@@ -0,0 +1,344 @@
|
|
|
|
|
+package com.sf._04_stream;
|
|
|
|
|
+
|
|
|
|
|
+import org.junit.Test;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
+
|
|
|
|
|
+public class StreamTest {
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 如何创建stream 刘
|
|
|
|
|
+ * 集合.stream();
|
|
|
|
|
+ * Arrays.stream(数组);
|
|
|
|
|
+ * Stream.of(元素,元素);
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test(){
|
|
|
|
|
+ // 1 通过集合获取stream 刘
|
|
|
|
|
+ List<Integer> list = new ArrayList<>(Arrays.asList(1,3,4,5,6,7,8));
|
|
|
|
|
+ Stream<Integer> stream = list.stream();
|
|
|
|
|
+ System.out.println(stream);
|
|
|
|
|
+ // 2 数组获取steram流
|
|
|
|
|
+ Integer[] arr = {2,3,4,5,6,7,8};
|
|
|
|
|
+ Stream<Integer> stream1 = Arrays.stream(arr);
|
|
|
|
|
+ System.out.println(stream1);
|
|
|
|
|
+ // 3 通过Stream.of 获取流信息
|
|
|
|
|
+ System.out.println(Stream.of(1, 2, 3, 4, 5, 67));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 中间操作
|
|
|
|
|
+ * filter(函数式接口 有参数有返回 boolean)
|
|
|
|
|
+ * 过滤大于3的元素
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test2(){
|
|
|
|
|
+ List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5));
|
|
|
|
|
+// for (Integer integer : list) {
|
|
|
|
|
+// if(integer>3){
|
|
|
|
|
+// System.out.println(integer);
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+ // 获取stream 刘
|
|
|
|
|
+ // list.stream() 开始
|
|
|
|
|
+ // filter: 中间处理
|
|
|
|
|
+ // forEach: 结束操作
|
|
|
|
|
+ list.stream().filter(e-> e>3).forEach(e-> System.out.println(e));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 现在有一个集合 zhangsan zhangsi lisi wangwu zhaoliu
|
|
|
|
|
+ * 找到行zhang的元素 并且打印
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test3(){
|
|
|
|
|
+ List<String> list = new ArrayList<>(Arrays.asList("zhangsan","lisi","zhangsi","wangwu","zhaoliu"));
|
|
|
|
|
+ list.stream().filter(e-> e.contains("zhang")).forEach(e-> System.out.println(e));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * distinct: 去除重复数据
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test4(){
|
|
|
|
|
+ ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(1,1,2,3,4,5,5,6,7));
|
|
|
|
|
+ // 去除arrayList 当中重复数据
|
|
|
|
|
+ // 去除大于4 的重复的数据
|
|
|
|
|
+ arrayList.stream().
|
|
|
|
|
+ filter(e-> e > 3).
|
|
|
|
|
+ distinct().
|
|
|
|
|
+ forEach(e-> System.out.println(e));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * limit : 保留多少个数据
|
|
|
|
|
+ * skip: 跳过多少条数据
|
|
|
|
|
+ *
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test5(){
|
|
|
|
|
+ ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(1,1,2,3,4,5,5,6,7));
|
|
|
|
|
+ // 保留前4 个数据
|
|
|
|
|
+// arrayList.stream().limit(4).forEach(e-> System.out.println(e));
|
|
|
|
|
+ arrayList.stream().
|
|
|
|
|
+ skip(3). // 跳过3条数据
|
|
|
|
|
+ limit(3). // 保留3个数据
|
|
|
|
|
+ forEach(e-> System.out.println(e));
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 1,1,9,3,4,5,5,6,9,13,15,12,12,18,21
|
|
|
|
|
+ * 去重
|
|
|
|
|
+ * 找出3的倍数的数字
|
|
|
|
|
+ * 每页显示3条数据, 展示第2页的信息
|
|
|
|
|
+ * 3 6 9 12 15 18 21
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test6(){
|
|
|
|
|
+ ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(1,1,9,3,4,5,5,6,9,13,15,12,12,18,21));
|
|
|
|
|
+ arrayList.stream().
|
|
|
|
|
+ distinct().
|
|
|
|
|
+ filter(e-> e%3 == 0).
|
|
|
|
|
+ skip(3).
|
|
|
|
|
+ limit(3).
|
|
|
|
|
+ forEach(e-> System.out.println(e));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * map: 主要作用是对于集合当中元素进行额外的处理
|
|
|
|
|
+ * 需要 集合元素 1 2 3 4 我们希望集合当中元素进行平方操作
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test7(){
|
|
|
|
|
+ ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(1,2,3,4));
|
|
|
|
|
+ arrayList.stream().
|
|
|
|
|
+ map(e-> e*e). // 对于之前元素进行平方处理
|
|
|
|
|
+ forEach(e-> System.out.println(e));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 集合当中元素 , i love you china
|
|
|
|
|
+ * I LOVE YOU CHAINA
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test8(){
|
|
|
|
|
+ ArrayList<String> arrayList = new ArrayList<>(Arrays.asList("i","love","you","china"));
|
|
|
|
|
+ arrayList.stream().
|
|
|
|
|
+ map(e-> e.toUpperCase()). // 对于之前元素进行平方处理
|
|
|
|
|
+ forEach(e-> System.out.println(e));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 定义一个Student id name age score
|
|
|
|
|
+ * 往list 集合当中添加6 个学生, 无需 , 先试用工具类按照score 进行降序排序
|
|
|
|
|
+ * 使用stream 查询年龄大于18 的学生,保留3个 , 将他们分数+10分
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test9(){
|
|
|
|
|
+ List<Student> students = new ArrayList<>();
|
|
|
|
|
+ students.add(new Student(1L,"zhangsan",19,90.0));
|
|
|
|
|
+ students.add(new Student(2L,"zhangsan1",17,91.0));
|
|
|
|
|
+ students.add(new Student(3L,"zhangsan2",16,90.0));
|
|
|
|
|
+ students.add(new Student(4L,"zhangsan3",19,93.0));
|
|
|
|
|
+ students.add(new Student(5L,"zhangsan4",20,95.0));
|
|
|
|
|
+ students.add(new Student(6L,"zhangsan5",21,80.0));
|
|
|
|
|
+ students.add(new Student(7L,"zhangsan6",13,99.0));
|
|
|
|
|
+ // 调用工具类进行分数排序
|
|
|
|
|
+ Collections.sort(students);
|
|
|
|
|
+ students.stream().
|
|
|
|
|
+ filter(s-> s.getAge() > 18).
|
|
|
|
|
+ map(s-> {
|
|
|
|
|
+ s.setScore(s.getScore()+10);
|
|
|
|
|
+ return s;
|
|
|
|
|
+ }).
|
|
|
|
|
+ limit(3). //保留3个
|
|
|
|
|
+ forEach(e-> System.out.println(e));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test10(){
|
|
|
|
|
+ List<Integer> list1 = Arrays.asList(1, 2);
|
|
|
|
|
+ List<Integer> list2 = Arrays.asList(3, 4, 5);
|
|
|
|
|
+
|
|
|
|
|
+ // 想要把两个集合当中数据放到一起然后进行处理
|
|
|
|
|
+ Stream.of(list1, list2).
|
|
|
|
|
+ flatMap(list-> list.stream()).
|
|
|
|
|
+ forEach(e-> System.out.println(e));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * ArrayList<Student> list1 3 个学生 list2 3个徐盛
|
|
|
|
|
+ * 需要把两个班级归类一起查看年龄>18 信息
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test11(){
|
|
|
|
|
+ ArrayList<Student> list1 = new ArrayList<>();
|
|
|
|
|
+ list1.add(new Student(1L,"zhangsan",19,80.0));
|
|
|
|
|
+ list1.add(new Student(2L,"zhangsan2",17,81.0));
|
|
|
|
|
+ list1.add(new Student(3L,"zhangsan3",16,82.0));
|
|
|
|
|
+
|
|
|
|
|
+ ArrayList<Student> list2 = new ArrayList<>();
|
|
|
|
|
+ list2.add(new Student(1L,"zhangsan5",19,80.0));
|
|
|
|
|
+ list2.add(new Student(2L,"zhangsan6",17,81.0));
|
|
|
|
|
+ list2.add(new Student(3L,"zhangsan7",16,82.0));
|
|
|
|
|
+
|
|
|
|
|
+ Stream.of(list1,list2).
|
|
|
|
|
+ flatMap(list -> list.stream()).
|
|
|
|
|
+ filter(s -> s.getAge()>18).
|
|
|
|
|
+ forEach(e-> System.out.println(e));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test12(){
|
|
|
|
|
+ List<Integer> list1 = Arrays.asList(1,3,2,6,5);
|
|
|
|
|
+ list1.stream().sorted().forEach(e-> System.out.println(e));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 想要从大到小进行降序排序
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test13(){
|
|
|
|
|
+ List<Integer> list1 = Arrays.asList(1,3,2,6,5);
|
|
|
|
|
+// list1.stream().sorted(new Comparator<Integer>() {
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public int compare(Integer o1, Integer o2) {
|
|
|
|
|
+// return o2 - o1;
|
|
|
|
|
+// }
|
|
|
|
|
+// }).forEach(e-> System.out.println(e));
|
|
|
|
|
+ list1.stream().sorted(((o1, o2) -> o2-o1)).forEach(e-> System.out.println(e));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * list<Student> 按照学生成绩进行降序排序 , 取前三名
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test14(){
|
|
|
|
|
+ List<Student> students = new ArrayList<>();
|
|
|
|
|
+ students.add(new Student(1L,"zhangsan",19,90.0));
|
|
|
|
|
+ students.add(new Student(2L,"zhangsan1",17,91.0));
|
|
|
|
|
+ students.add(new Student(3L,"zhangsan2",16,90.0));
|
|
|
|
|
+ students.add(new Student(4L,"zhangsan3",19,93.0));
|
|
|
|
|
+ students.add(new Student(5L,"zhangsan4",20,95.0));
|
|
|
|
|
+ students.add(new Student(6L,"zhangsan5",21,80.0));
|
|
|
|
|
+ students.add(new Student(7L,"zhangsan6",13,99.0));
|
|
|
|
|
+
|
|
|
|
|
+ students.stream().
|
|
|
|
|
+ sorted((o1, o2) -> (int) (o2.getScore()-o1.getScore())). // 按照分数降序排序
|
|
|
|
|
+ limit(3). //保留前3名
|
|
|
|
|
+ forEach(e-> System.out.println(e));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 需求1: 获取给定一个集合数据的总数
|
|
|
|
|
+ *
|
|
|
|
|
+ * 需求2: 获取给定一个集合中的最大值
|
|
|
|
|
+ *
|
|
|
|
|
+ * 需求3: 获取给定一个集合中的最小值
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test15(){
|
|
|
|
|
+ List<Integer> list1 = Arrays.asList(1,3,2,6,5);
|
|
|
|
|
+ System.out.println(list1.stream().filter(e-> e> 3).count());
|
|
|
|
|
+
|
|
|
|
|
+ // 获取集合当中最大值 和最小值
|
|
|
|
|
+ System.out.println(list1.stream().min((o1, o2) -> o1- o2).get());
|
|
|
|
|
+ System.out.println(list1.stream().max((o1, o2) -> o1- o2).get());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * List 集合 查询分数大于90分学生有多少个人
|
|
|
|
|
+ * 查询班级分数最高同学
|
|
|
|
|
+ * 查询班级分数最低同学
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test16(){
|
|
|
|
|
+ List<Student> students = new ArrayList<>();
|
|
|
|
|
+ students.add(new Student(1L,"zhangsan",19,90.0));
|
|
|
|
|
+ students.add(new Student(2L,"zhangsan1",17,91.0));
|
|
|
|
|
+ students.add(new Student(3L,"zhangsan2",16,90.0));
|
|
|
|
|
+ students.add(new Student(4L,"zhangsan3",19,85.0));
|
|
|
|
|
+ students.add(new Student(5L,"zhangsan4",20,95.0));
|
|
|
|
|
+ students.add(new Student(6L,"zhangsan5",21,80.0));
|
|
|
|
|
+
|
|
|
|
|
+ System.out.println(students.stream().filter(s -> s.getScore() > 90).count());
|
|
|
|
|
+ System.out.println(students.stream().max((o1, o2) -> (int) (o1.getScore() - o2.getScore())).get());
|
|
|
|
|
+ System.out.println(students.stream().min((o1, o2) -> (int) (o1.getScore() - o2.getScore())).get());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * collect(Collectors.toSet(),Collection.toList(),toMap())
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test17(){
|
|
|
|
|
+ List<Integer> list = Arrays.asList(1,2,3,4,4,5,6);
|
|
|
|
|
+ //list->set
|
|
|
|
|
+ Set<Integer> set = list.stream().collect(Collectors.toSet());
|
|
|
|
|
+ System.out.println(set);
|
|
|
|
|
+ //set->list
|
|
|
|
|
+ List<Integer> list1 = set.stream().collect(Collectors.toList());
|
|
|
|
|
+ System.out.println(list1);
|
|
|
|
|
+ //list->map
|
|
|
|
|
+
|
|
|
|
|
+ List<Integer> list2 = Arrays.asList(1,2,3,4);
|
|
|
|
|
+ Map<Integer, Integer> map = list2.stream().distinct().collect(Collectors.toMap(e -> e, e -> e * e));
|
|
|
|
|
+ System.out.println(map);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * List<Student>
|
|
|
|
|
+ * 按照分数排序
|
|
|
|
|
+ * 查询名字中包含zhang
|
|
|
|
|
+ * 需要让他年龄+5
|
|
|
|
|
+ * 保留3
|
|
|
|
|
+ * 最终要把转成map (key=studentId, value =student)
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test18(){
|
|
|
|
|
+ List<Student> students = new ArrayList<>();
|
|
|
|
|
+ students.add(new Student(1L,"zhangsan",19,90.0));
|
|
|
|
|
+ students.add(new Student(2L,"zhangsan1",17,91.0));
|
|
|
|
|
+ students.add(new Student(3L,"zhangsan2",16,90.0));
|
|
|
|
|
+ students.add(new Student(4L,"zhangsan3",19,85.0));
|
|
|
|
|
+ students.add(new Student(5L,"zhangsan4",20,95.0));
|
|
|
|
|
+ students.add(new Student(6L,"zhangsan5",21,80.0));
|
|
|
|
|
+
|
|
|
|
|
+ Map<Long, Student> map = students.stream().
|
|
|
|
|
+ sorted((o1, o2) -> (int) (o2.getScore() - o1.getScore())). // 按照分数降序排序
|
|
|
|
|
+ filter(s -> s.getName().contains("zhang")). // 名字当中是否包含zhang
|
|
|
|
|
+ map(s -> { // 讲年龄+5
|
|
|
|
|
+ s.setAge(s.getAge() + 5);
|
|
|
|
|
+ return s;
|
|
|
|
|
+ }).
|
|
|
|
|
+ limit(3). // 取前3个
|
|
|
|
|
+ collect(Collectors.toMap(e -> e.getId(), e -> e));// 转成map
|
|
|
|
|
+ System.out.println(map);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * .collect(Collectors.summingInt(e -> e));
|
|
|
|
|
+ * 求平均 求和
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void test19(){
|
|
|
|
|
+ List<Integer> list = Arrays.asList(1,2,3,4,4,5,6);
|
|
|
|
|
+ IntSummaryStatistics collect = list.stream().collect(Collectors.summarizingInt(e -> 2*e));
|
|
|
|
|
+ System.out.println("总和为"+collect.getSum());
|
|
|
|
|
+ System.out.println("平均值:"+collect.getAverage());
|
|
|
|
|
+ System.out.println("最大值:"+collect.getMax());
|
|
|
|
|
+ System.out.println("最小值为:"+collect.getMin());
|
|
|
|
|
+ }
|
|
|
|
|
+}
|