参考: https://www.jianshu.com/p/007c76a417ab
https://www.runoob.com/java/java8-streams.html
https://blog.csdn.net/y_k_y/article/details/84633001
https://blog.csdn.net/yy1098029419/article/details/89452380
https://zhuanlan.zhihu.com/p/265884828
java 8 中新增了两个流方法,分别是Stream() 和 parallelStream()(并行流)
和iterator(迭代器)的区别:
Iterator,只能显式地一个一个遍历元素并对其执行某些 *** 作,
而Stream只要给出对其元素的 *** 作,Stream会吟诗的在内部进行遍历,做出相应的 *** 作;
流 *** 作的常用方法:
1,获取流
// 1 Collection 获取流 Listlist = new ArrayList<>(); Stream stream1 = list.stream(); // Map获取流 Map map = new HashMap<>(); Stream keyStream = map.keySet().stream(); Stream valueStream = map.values().stream(); Stream > entryStream = map.entrySet().stream(); // 根据数组获取流 String[] array = { "张无忌", "张翠山", "张三丰", "张一元" }; Stream stream = Stream.of(array);
2 流的 *** 作
1).单元素 *** 作:
Listnames = Arrays.asList("1张三","2赵四","3张四","4李四","5王五","6赵柳","7张六","8王少","9赵四","10张仁","11李星"); List list = names.stream() .filter(x -> x.endsWith("四")) // 过滤 .skip(1) // 跳过n元素,配合limit(n)可实现分页 .limit(7) //获取n个元素 .distinct() // 去重 .map(x -> x + "123") //接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。 .distinct() //去重 .sorted() //排序 升序 // .sorted(Comparator.reverseOrder()) //排序 降序 .collect(Collectors.toList()); // .count(); // 返回流中元素的总个数 // .max(); // 返回流中元素最大值 // .min() ; // 返回流中元素最小值 // .findFirst(); // 返回流中第一个元素 // .findAny(); //返回流中的任意元素 System.out.println(list);
2)对象 *** 作
参考: https://blog.csdn.net/xy3233/article/details/117931652
https://blog.csdn.net/y_k_y/article/details/84633001
@Data class Student { public Student(String name, Integer age) { this.age = age; this.name = name; } private String name; private Integer age; } @Test public void demo2(){ Listlist = new ArrayList<>(); list.add(new Student("张三", 3)); list.add(new Student("王二", 2)); list.add(new Student("麻子", 5)); list.add(new Student("李四", 4)); list.add(new Student("李明", 6)); list.add(new Student("李明", 1)); list.add(new Student("李明", 1)); List orderList = list.stream() .filter(x->x.name.startsWith("李")) // 过滤 .distinct() // 去重 属性都一样 才去重 // .sorted(Comparator.comparing(Student::getAge)) //按年纪升序排序 .sorted(Comparator.comparing(Student::getAge).reversed()) //降序 或者使用 Collections.reverse(orderList) .collect(Collectors.toList()); System.out.println(orderList); // 按姓名去重 ArrayList studentArrayList = list.stream(). collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getName))), ArrayList::new)); System.out.println(studentArrayList); // 年龄的集合 List collect = studentArrayList.stream() .map(x -> x.age) // 或 .collect(Collectors.toList()); System.out.println(collect); // 最大年龄 Student student = studentArrayList.stream().max(Comparator.comparing(Student::getAge)).get(); System.out.println(student); //平均年龄 Double averageAge = list.stream().collect(Collectors.averagingInt(Student::getAge)); System.out.println(averageAge); // 按年龄分组 Map > collect1 = list.stream().collect(Collectors.groupingBy(Student::getAge)); System.out.println(collect1); // 按年龄 姓名 成为map Map collect2 = list.stream() .distinct() .collect(Collectors.toMap(Student::getAge, Student::getName)); System.out.println(collect2); // 年龄 对象 成map Map collect3 = list.stream() .distinct() .collect(Collectors.toMap(Student::getAge, student1 -> student1)); System.out.println(collect3); } // 使用map 创建一个全新的list List collect4 = list.stream() .map(x -> x.age > 3 ? new Student("张三", x.age) : new Student("李四", x.age)) .collect(Collectors.toList()); System.out.println(collect4);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)