|
@@ -0,0 +1,317 @@
|
|
|
+package com.sf.day24._03_stream;
|
|
|
+
|
|
|
+import com.sf.day24._03_stream.dt.Product;
|
|
|
+import com.sf.day24._03_stream.dt.User;
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Stream;
|
|
|
+
|
|
|
+public class StreamTest {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 需求:
|
|
|
+ * 操作1:对每个元素求平方
|
|
|
+ * 操作2:请找出数组中的偶数元素
|
|
|
+ * 操作3:对偶数进行升序排序
|
|
|
+ * 操作4:打印
|
|
|
+ * ArrayList(1,3,2,4) (1,9,4,16) ->(4,16)
|
|
|
+ */
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test1(){
|
|
|
+ //1 先定义一个集合
|
|
|
+ List<Integer> list = Arrays.asList(1, 3, 2, 4);
|
|
|
+ Collections.sort(list);
|
|
|
+ //2 遍历拿到结合当中每一个元素
|
|
|
+ List<Integer> list1 = new ArrayList<>();
|
|
|
+ list.forEach(e ->{
|
|
|
+ //3 拿集合当中元素进行平方 c = e * e
|
|
|
+ Integer pow = e * e;
|
|
|
+ //4 判断是否是偶数 if(c%2 == 0) 添加一个新的集合中
|
|
|
+ if(pow % 2 == 0){
|
|
|
+ //5 调用Collections.sort(list) 进行排序
|
|
|
+ list1.add(pow);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ System.out.println(list1);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * jdk 提供的4个函数式接口
|
|
|
+ * Consumer 有参数无返回值
|
|
|
+ * Supplier 无参数有返回值
|
|
|
+ * Function 有参数有返回值
|
|
|
+ * Predicaete 有参数有返回 返回boolean
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void test2(){
|
|
|
+ // 1 创建Stream 流
|
|
|
+ List<Integer> list = Arrays.asList(1, 3, 2, 4);
|
|
|
+ // stream 流他是支持链式编程 x.x.x.x.x
|
|
|
+ list.stream(). // 1 创建steram 流
|
|
|
+ map(e -> e * e). // map 其实就是对于集合元素要进行什么样处理 中间操作
|
|
|
+ filter(e -> e % 2 == 0). // filter表示过滤, 过滤偶数 中间操作
|
|
|
+ sorted(). // 这个叫做排序 中间操作
|
|
|
+ forEach(e -> System.out.println(e)); // 遍历
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建Steram 流三种方式
|
|
|
+ * 集合.steram()
|
|
|
+ * Arrays.stream()
|
|
|
+ * Stream.of(元素)
|
|
|
+ *
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void test3(){
|
|
|
+ // 集合创建Stream
|
|
|
+ List<Integer> list = Arrays.asList(1, 2, 3, 3, 4);
|
|
|
+ // 遍历集合.foreach 没有中间操作就原样打印
|
|
|
+// list.stream().forEach(System.out::println);
|
|
|
+
|
|
|
+ // 使用数组创建Stream
|
|
|
+ Integer[] arr = {1,2,3,3,4,5};
|
|
|
+ Stream<Integer> stream = Arrays.stream(arr);
|
|
|
+ stream.forEach(System.out::println);
|
|
|
+
|
|
|
+ // 使用Stream.of(1,2,3,4,5)
|
|
|
+ Stream.of(1,2,3,34,5).forEach(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test4(){
|
|
|
+ // 已知有这样集合{1,2,3,3,4,5,6,7}
|
|
|
+ // 要求求出大于3 有哪些数据
|
|
|
+ List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
|
|
+ // 创建stream流
|
|
|
+ list.stream().filter(e-> e>3).forEach(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 需求: List<String> abc a abcd cd
|
|
|
+ * 要求找出集合当中包含 ab,进行遍历
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void tes5(){
|
|
|
+ List<String> list = Arrays.asList("abc", "a", "abcd", "cd");
|
|
|
+ list.stream().filter(e-> e.contains("ab")).forEach(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 需求: List<String> abc dd abcd abc a abcd cd
|
|
|
+ * 要求找出集合当中包含 ab,进行遍历,并且要去重
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void test6(){
|
|
|
+ List<String> list = Arrays.asList("abc","abcd","abc", "a", "abcd", "cd");
|
|
|
+ list.stream().
|
|
|
+ filter(e-> e.contains("ab")).
|
|
|
+ distinct().
|
|
|
+ forEach(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 需求: List<String> abc,abe,cabc, dd abcd abc a abcd cd
|
|
|
+ * 要求找出集合当中包含 ab,进行遍历,并且要去重
|
|
|
+ * 值获取2个
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void test7(){
|
|
|
+ List<String> list = Arrays.asList("abc","abe","cabc","abcd","abc", "a", "abcd", "cd");
|
|
|
+ list.stream().
|
|
|
+ filter(e-> e.contains("ab")). // filter 表示过滤
|
|
|
+ distinct(). // 对数据进行去重
|
|
|
+ limit(3). // 保留多少个元素
|
|
|
+ forEach(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 需求: List<String> abc,abe,cabc, dd abcd abc a abcd cd
|
|
|
+ * 要求找出集合当中包含 ab,进行遍历,并且要去重
|
|
|
+ * 保留3个
|
|
|
+ * 跳过第一个
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void test8(){
|
|
|
+ List<String> list = Arrays.asList("abc","abe","cabc","abcd","abc", "a", "abcd", "cd");
|
|
|
+ list.stream().
|
|
|
+ filter(e-> e.contains("ab")). // filter 表示过滤
|
|
|
+ distinct(). // 对数据进行去重
|
|
|
+ limit(3). // 保留多少个元素
|
|
|
+ skip(1). // 跳过几个元素
|
|
|
+ forEach(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 需求: 现在有List<Product> 集合 id name price
|
|
|
+ * 结合当中有5个元素(1L,小米手机,1000) (2L,手表, 5000) ,(3L,"华为手机",2000)
|
|
|
+ * (4L,小米电脑,2000) (5L,"苹果手机",4000-) (6L,"金立手机",5000-)
|
|
|
+ *
|
|
|
+ * 要求1 要求查询是手机 价格大于1000
|
|
|
+ * 保留2个元素
|
|
|
+ *
|
|
|
+ *
|
|
|
+
|
|
|
+ */
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test9(){
|
|
|
+
|
|
|
+ List<Product> products = Arrays.asList(
|
|
|
+ new Product(1L, "小米手机", 1000),
|
|
|
+ new Product(2L, "华为手机", 2000),
|
|
|
+ new Product(3L, "小米电脑", 2000),
|
|
|
+ new Product(4L, "苹果手机", 4000),
|
|
|
+ new Product(5L, "金立手机", 5000),
|
|
|
+ new Product(6L, "三星手机", 1500)
|
|
|
+ );
|
|
|
+ products.stream().
|
|
|
+ filter(e-> e.getName().contains("手机") && e.getPrice() >1000).
|
|
|
+ limit(2).
|
|
|
+ forEach(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 需求2 : List<String> list {"a",'b","c","d","e","f","g"}
|
|
|
+ * 要求打印d 和 e
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void test10(){
|
|
|
+ List<String> list = Arrays.asList("a", "b", "c", "d", "e", "f", "g");
|
|
|
+ list.stream().skip(3).limit(2).forEach(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 已知条件 ArrayList ("abc","bcd","cc","ddd")
|
|
|
+ * 要求: 把结合当中每一个元素都变成大写
|
|
|
+ * 解决: 使用map (额外处理元素)
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void test11(){
|
|
|
+ List<String> list = Arrays.asList("abc", "bcd", "Cc", "ddd");
|
|
|
+ // function 函数式接口 有参有返回
|
|
|
+ list.stream().
|
|
|
+ map(e -> e.toUpperCase()).
|
|
|
+ forEach(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 需求: ArrayList ("abc","bcd","cc","ddd")
|
|
|
+ * 求出集合当中所有字符串长度
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void test12(){
|
|
|
+ List<String> list = Arrays.asList("abc", "bcd", "Cc", "ddd");
|
|
|
+ list.stream().map(e->e.length()).forEach(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 需求: 已知 集合[1,2,2,3,5,6,7,8,9]
|
|
|
+ * 要求将集合当中的每一个元素 加10 map
|
|
|
+ * 要求过滤出来 大于15的元素,并且不能有重复 filter distinct
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void test14(){
|
|
|
+ List<Integer> list = Arrays.asList(1, 2, 2, 3, 5, 6, 7, 8, 9);
|
|
|
+ list.stream().
|
|
|
+ map(e-> e+10).
|
|
|
+ filter(e-> e>15).
|
|
|
+ distinct().
|
|
|
+ forEach(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 需求: 已知有一个List<User> 集合 User id name age intergral
|
|
|
+ * (1L,"zhang_san",10,10) (1L,"li_si",20,20) (1L,"wang_wu",30,10)
|
|
|
+ * 要求过滤出来年龄大于18岁的
|
|
|
+ * 要求吧名字变成ZS LS WW
|
|
|
+ * 如果年龄大于21岁就加10积分
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void test15(){
|
|
|
+ List<User> list = Arrays.asList(
|
|
|
+ new User(1L, "zhang_san", 10, 10),
|
|
|
+ new User(1L, "li_si", 20, 20),
|
|
|
+ new User(1L, "wang_wu", 30, 10)
|
|
|
+ );
|
|
|
+ list.stream().
|
|
|
+ filter(e-> e.getAge()>18).
|
|
|
+ map(e->{
|
|
|
+ //1 获取user 的name
|
|
|
+ String name = e.getName();
|
|
|
+ //2 按照_ 进行分割 [zhangsan,san]
|
|
|
+ String[] arr = name.split("_");
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (String s : arr) {
|
|
|
+ // zhangsan san
|
|
|
+ // z-第一个字母-> 大写 s - 第一个字母->大写
|
|
|
+ sb.append(s.substring(0,1).toUpperCase());
|
|
|
+ }
|
|
|
+ //3 把名字重新设置到对象中
|
|
|
+ e.setName(sb.toString());
|
|
|
+ return e;
|
|
|
+ }).
|
|
|
+ map(e-> {
|
|
|
+ if(e.getAge()>21){
|
|
|
+ e.setIntegal(e.getIntegal()+10);
|
|
|
+ }
|
|
|
+ return e;
|
|
|
+ }).
|
|
|
+ forEach(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 需求: 已知 {1,2,4,6,3}
|
|
|
+ * 从小到大进行排序
|
|
|
+ *
|
|
|
+ * 如果我想要倒序如何操作呢?
|
|
|
+ * 默认是sorted 是从小到大进行排序的
|
|
|
+ * 有其他排序规则 那要调用有参构造器
|
|
|
+ * sorted(比较器)
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void test16(){
|
|
|
+ List<Integer> list = Arrays.asList(1, 2, 4, 6, 3);
|
|
|
+// list.stream().sorted().forEach(System.out::println);
|
|
|
+// list.stream().sorted(new Comparator<Integer>() {
|
|
|
+// @Override
|
|
|
+// public int compare(Integer o1, Integer o2) {
|
|
|
+// return o2-o1;
|
|
|
+// }
|
|
|
+// }).forEach(System.out::println);
|
|
|
+ list.stream().sorted((o1,o2)-> o2-o1).forEach(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 需求
|
|
|
+ //需求: 已知有一个List<User> 集合 User id name age intergral
|
|
|
+ //(1L,"zhang_san",10,10) (1L,"li_si",20,30) (1L,"wang_wu",30,15) (1L,"wang_ba",30,20)
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test(){
|
|
|
+ List<User> list = Arrays.asList(
|
|
|
+ new User(1L, "zhang_san", 10, 5),
|
|
|
+ new User(2L, "li_si", 20, 30),
|
|
|
+ new User(3L, "wang_wu", 30, 10),
|
|
|
+ new User(4L, "wang_ba", 30, 20)
|
|
|
+ );
|
|
|
+ // 1 要求1 按照积分降序
|
|
|
+ // 2 筛选出来名字带有wang
|
|
|
+ // 3 把小写wang变成大写的WANG
|
|
|
+ // 4 保留2 个
|
|
|
+ list.stream().
|
|
|
+ sorted((o1,o2)-> o2.getIntegal()-o1.getIntegal()).
|
|
|
+ filter(e-> e.getName().contains("wang")).
|
|
|
+ map(e->{
|
|
|
+ String name = e.getName().toUpperCase();
|
|
|
+ e.setName(name);
|
|
|
+ return e;
|
|
|
+ }).
|
|
|
+ limit(1).
|
|
|
+ forEach(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|