JDK1.8新特性---Stream API

以下内容均来自如下参考链接:

1. 定义

       Stream 是 Java8 中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,可以执行非常复杂的查找、过滤和映射数据等操作。使用Stream API 对集合数据进行操作,就类似于使用 SQL 执行的数据库查询。也可以使用 Stream API 来并行执行操作。

       StreamAPI位于java.util.stream包中,可以帮助我们更好地对数据进行集合操作,其本质就是对数据的操作进行流水线式处理,也可以理解为一个更加高级的迭代器,主要作用是遍历集合中的每一个元素。简而言之,StreamAP提供了一种高效且易于使用的处理数据方式。

2. 特点

Stream(流):是数据渠道,用于操作数据源(集合、数组等)所生成的元素序列。 集合讲的是数据,流讲的是计算!

  • Stream自己不会存储数据。

  • Stream不会改变源对象。相反,它们会返回一个持有结果的新Stream对象。

  • Stream操作是延迟执行的。这就意味着它们等到有结果的时候,才会执行。

       和list不同,Stream代表的是任意Java对象的序列,且stream输出的元素可能并没有预先存储在内存中,而是实时计算出来的。它可以“存储”有限个或无限个元素。例如:我们想表示一个全体自然数的集合,使用list是不可能写出来的,因为自然数是无限的,不管内存多大,也没法放到list中,但是使用Sream就可以。

3. 使用步骤

  • 创建Stream:一个数据源(例如:set 、list),获取一个流

  • 中间操作:一个中间操作链,对数据源的数据进行处理

  • 终止操作/终端操作: 一个终止操作,执行中间操作链,并产生结果。

3.1 创建流

创建流方式有多种:

  • 第一种:通过集合

Java8中的Collection接口被扩展,提供了两个获取流的方法:

 default Stream<E> stream() : 返回一个顺序流
 default Stream<E> parallelStream() : 返回一个并行流 

对于Collection接口(List 、Set、Queue等)直接调用Stream()方法可以获取Stream:

 List<String> list = new ArrayList<>();
 Stream<String> stringStream = list.stream(); //返回一个顺序流
 Stream<String> parallelStream = list.parallelStream(); //返回一个并行流(可多线程)
  • 第二种:通过数组

Java8 中的 Arrays 的静态方法 stream() 可以获取数组流:

  static <T> Stream<T> stream(T[] array): 返回一个流

重载形式,能够处理对应基本类型的数组:

 public static IntStream stream(int[] array)
 public static LongStream stream(long[] array)
 public static DoubleStream stream(double[] array) 

把数组变成Stream使用Arrays.stream()方法:

 Stream<String> stream = Arrays.stream(new String[]{"CBB", "YJJ", "CB", "CJJ"});
  • 第三种:由值创建流

可以使用静态方法 Stream.of(),通过显式值创建一个流。它可以接收任意数量的参数。

 public static<T> Stream<T> of(T... values) : 返回一个流 

举例:

 Stream<String> stream = Stream.of("A", "B", "C", "D");
  • 第四种:由函数创建流

创建无限流可以使用静态方法 Stream.iterate()和Stream.generate():

 public static<T> Stream<T> iterate(final T seed, finalUnaryOperator<T> f)  迭代
 public static<T> Stream<T> generate(Supplier<T> s)  生成

举例:

 // 迭代遍历10个奇数
 Stream.iterate(1,t->t+2).limit(10).forEach(System.out::println);
 ​
 //生成10个随机数
 Stream.generate(Math::random).limit(10).forEach(System.out::println);

输出结果:

 1
 3
 5
 7
 9
 11
 13
 15
 17
 19
 0.29319403991837045
 0.5662087080300633
 0.3673016807205197
 0.2251147085605929
 0.7034620532932137
 0.35966619892157714
 0.8589438168844887
 0.14008847657551204
 0.6480066782940369
 0.3681492616232719
  • 第五种:自己构建

  • 第六种:其他等等

测试代码:

 public class MyTest2 {
     public static void main(String[] args) {
         // Stream 的操作三个步骤
         // 1. 创建 Stream
         //    一个数据源(如:集合、数组),获取一个流
         // 2. 中间操作
         //    一个中间操作链,对数据源的数据进行处理
         // 3. 终止操作(终端操作)
         //    一个终止操作,执行中间操作链,并产生结果
  
         //1.创建 Stream
         //  一个数据源(如:集合、数组),获取一个流
         List<Integer> list = Arrays.asList(10, 20, 30, 70, 80);
         //创建流的方式1 集合中的 stream()方法
         Stream<Integer> stream = list.stream();
  
         Integer[] arr = new Integer[]{20, 30, 80, 60, 80};
         //方式2
         //Arrays类中的 stream(arr); 方法
         Stream<Integer> stream1 = Arrays.stream(arr);
  
         //方式3:
         Stream<Integer> integerStream = Stream.of(20, 20, 30, 85, 95, 85);
  
         //创建无限流
         Stream<Double> generate = Stream.generate(Math::random);
  
         //generate.forEach(System.out::println);
  
         //创建
         Stream<Integer> iterate = Stream.iterate(0, new UnaryOperator<Integer>() {
             @Override
             public Integer apply(Integer integer) {
                 return integer += 1;
             }
         });
  
         iterate.forEach(System.out::println);
     }
 }

3.2 中间操作

       多个中间操作可以连接起来形成一个流水线,除非流水线上触发终止操作,否则中间操作不会执行任何的处理!而在终止操作时一次性全部处理,称为“惰性求值”。

       一个流后面可以跟随着0个或者多个中间操作,其目的是打开流,做出某种程度的数据过滤、去重、排序、映射、跳过等,然后返回一个新的流,交给下一个使用。仅仅是调用这个方法,没有真正开始遍历。主要操作:map (mapToInt, flatMap 等)、 filter、 distinct、 sorted、 peek、 limit、 skip、 parallel、 sequential、 unordered等。

3.2.1 筛选与切片

  • filter(Predicate p):过滤,接收Lambda, 从流中排除某些元素。

  • distinct():去重,通过流所生成元素的hashCode()和equals()去除重复元素。

  • limit(long maxSize):截断流,使其元素不超过给定数量。

  • skip(long n):跳过元素,返回一个扔掉了前 n 个元素的流。若流中元素不足 n 个,则返回一个空流,与 limit(n) 互补。

3.2.2 映射

  • map(Function f):接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。

  • mapToDouble(ToDoubleFunction f):接收一个函数作为参数,该函数会被应用到每个元素上,产生一个新的DoubleStream。

  • mapToInt(ToIntFunction f):接收一个函数作为参数,该函数会被应用到每个元素上,产生一个新的 IntStream。

  • mapToLong(ToLongFunction f):接收一个函数作为参数,该函数会被应用到每个元素上,产生一个新的 LongStream。

3.2.3 排序

  • sorted() : 产生一个新流,其中按自然顺序排序元素,实现Compareble接口

  • sorted(Comparator comp) :产生一个新流,其中按比较器顺序排序,传入一个比较

3.3 终止操作

       一个终止操作,执行中间操作链,产生结果。

       终端操作会从流的流水线生成结果。其结果可以是任何不是流的值,例如:List、Integer,甚至是 void 。

主要操作:forEach、 forEachOrdered、 toArray、 reduce、 collect、 min、 max、 count、 anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 iterator等。

举例:

 public class MyTest {
     public static void main(String[] args) {
         List<Employee> list = Arrays.asList(
                 new Employee(102, "李四", 59, 6666.66),
                 new Employee(101, "张三", 18, 9999.99),
                 new Employee(103, "王五", 28, 3333.33),
                 new Employee(104, "赵六", 8, 7777.77),
                 new Employee(104, "赵六", 8, 7777.77),
                 new Employee(104, "赵六", 8, 7777.77),
                 new Employee(105, "田七", 38, 5555.55)
         );
         Stream<Employee> stream = list.stream().filter(emp -> {
             System.out.println("中间操作是否执行");
             return emp.getSalary() > 3000;
         });
         //没有终止操作,中间的操作就不执行,体现的就是延迟加载思想
         stream.forEach(System.out::println);
     }
 }

3.3.1 查找与匹配

  • allMatch(Predicate p):检查是否匹配所有元素,比如判断所有员工的年龄都是17岁,如果有一个不是,就返回false;

  • anyMatch(Predicate p):检查是否至少匹配一个元素,比如判断是否有姓王的员工,如果至少有一个,就返回true;

  • noneMatch(Predicate p):检查是否没有匹配所有元素 employee.getSalary() < 3000; 每个员工的工资如果都高于3000,就返回true;如果有一个低于3000,就返回false;

  • findFirst():返回第一个元素,比如获取工资最高的人或者获取工资最高的值

  • findAny():返回当前流中的任意元素,比如随便获取一个姓王的员工count(),返回流中元素总数

  • max(Comparator c):返回流中最大值,比如获取最大年龄值

  • min(Comparator c):返回流中最小值,比如获取最小年龄的值

  • forEach(Consumer c):内部迭代(使用Collection接口需要用户去做迭代,称为外部迭代,相反,Stream API 使用内部迭代---它帮你把迭代做了)。

举例:

 public class MyTest2 {
     public static void main(String[] args) {
         /*allMatch(Predicate p) 检查是否匹配所有元素 比如判断 所有员工的年龄都是17岁 如果有一个不是, 就返回false
         anyMatch(Predicate p) 检查是否至少匹配一个元素 比如判断是否有姓王的员工, 如果至少有一个就返回true
         noneMatch(Predicate p) 检查是否没有匹配所有元素 employee.getSalary() < 3000;
         每个员工的工资如果都高于3000就返回true 如果有一个低于3000 就返回false
         findFirst() 返回第一个元素 比如获取工资最高的人 或者 获取工资最高的值是
         findAny() 返回当前流中的任意元素 比如随便获取一个姓王的员工
         count() 返回流中元素总数
         max(Comparator c) 返回流中最大值 比如:获取最大年龄值
         min(Comparator c) 返回流中最小值 比如:获取最小年龄的值*/
         List<Employee> list = Arrays.asList(
                 new Employee(102, "李四", 59, 6666.66),
                 new Employee(101, "张三", 18, 9999.99),
                 new Employee(103, "王五", 28, 3333.33),
                 new Employee(104, "赵六", 8, 7777.77),
                 new Employee(104, "赵六", 8, 7777.77),
                 new Employee(104, "赵六", 8, 7777.77),
                 new Employee(105, "田七", 38, 5555.55)
         );
  
         boolean b = list.stream().allMatch(employee -> employee.getAge() >= 18);
         System.out.println(b);
  
         boolean b2 = list.stream().anyMatch(employee -> employee.getAge() >= 18);
  
         System.out.println(b2);
  
         Optional<Employee> first = list.stream().sorted((x, y) -> (int) (x.getSalary() - y.getSalary())).findFirst();
         Employee employee = first.get();
         System.out.println(employee);
  
         long count = list.stream().distinct().count();
         System.out.println(count);
  
         Optional<Employee> max = list.stream().max(new Comparator<Employee>() {
             @Override
             public int compare(Employee a, Employee b) {
                 return a.getAge() - b.getAge();
             }
         });
         Employee employee1 = max.get();
         System.out.println(employee1);
     }
 }

3.3.2 归纳

  • reduce(T iden, BinaryOperator b):参1是起始值, 参2是二元运算,可以将流中元素反复结合起来,得到一个值,返回 T,比如求集合中元素的累加总和;

  • reduce(BinaryOperator b) :这个方法没有起始值,可以将流中元素反复结合起来,得到一个值,返回 Optional<T> , 比如你可以算所有员工工资的总和;

  • 备注:map和 reduce 的连接通常称为 map-reduce 模式,因Google 用它来进行网络搜索而出名。

举例:

 public class MyTest3 {
     public static void main(String[] args) {
         List<Integer> list = Arrays.asList(10, 20, 30, 70, 80);
         Integer reduce = list.stream().reduce(0, new BinaryOperator<Integer>() {
             @Override
             public Integer apply(Integer a, Integer b) {
                 return a + b;
             }
         });
         System.out.println(reduce);// 求总和:210
     }
 }

优化后:

 public class MyTest3 {
     public static void main(String[] args) {
         List<Integer> list = Arrays.asList(10, 20, 30, 70, 80);
         Integer reduce = list.stream().reduce(0, (a,b)->a + b);
         System.out.println(reduce);
     }
 }

3.3.3 收集

  • collect(Collector c) :将流转换为其他形式,接收一个Collector接口的实现,用于给Stream中元素做汇总的方法。

  • Collector接口中方法的实现决定了如何对流执行收集操作(如收集到 List、Set、Map)。

  • 但是 Collectors 实现类提供了很多静态方法,可以方便地创建常见收集器实例。

具体方法与实例如下:

 public class MyTest4 {
     public static void main(String[] args) {
         List<Employee> list = Arrays.asList(
                 new Employee(102, "李四", 59, 6666.66),
                 new Employee(101, "张三", 18, 9999.99),
                 new Employee(103, "王五", 28, 3333.33),
                 new Employee(104, "赵六", 8, 7777.77),
                 new Employee(104, "赵六", 8, 7777.77),
                 new Employee(104, "赵六", 8, 7777.77),
                 new Employee(105, "田七", 38, 5555.55)
         );
  
         // List<String> collect = list.stream().map(e -> e.getName()).collect(Collectors.toList());
         // System.out.println(collect);
  
         Set<String> collect = list.stream().map(e -> e.getName()).collect(Collectors.toSet());
         System.out.println(collect);
     }
 }

3.3.4 Collectors 中的方法

  • List<T> toList():把流中元素收集到List。比如把所有员工的名字通过map()方法提取出来,放到List集合中去。 例子:List<Employee> emps= list.stream().map(提取名字).collect(Collectors.toList());

  • Set<T> toSet() :把流中元素收集到Set。比如把所有员工的名字通过map()方法提取出来,放到Set集合中去。 例子:Set<Employee> emps= list.stream().map(提取名字).collect(Collectors.toSet());

  • Collection<T> toCollection():把流中元素收集到创建的集合。比如把所有员工的名字通过map()方法提取出来,放到自己指定的集合中去。 例子:Collection<Employee> emps=list.stream().map(提取名字).collect(Collectors.toCollection(ArrayList::new));

  • Long counting():计算流中元素的个数。 例子:long count = list.stream().collect(Collectors.counting());

  • Integer summingInt():对流中元素的整数属性求和。 例子:inttotal=list.stream().collect(Collectors.summingInt(Employee::getSalary));

  • Double averagingInt():计算流中元素Integer属性的平均值。 例子:doubleavg= list.stream().collect(Collectors.averagingInt(Employee::getSalary));

  • IntSummaryStatistics summarizingInt():收集流中Integer属性的统计值。 例子:DoubleSummaryStatistics dss= list.stream().collect(Collectors.summarizingDouble(Employee::getSalary)); 从DoubleSummaryStatistics 中可以获取最大值,平均值等 double average = dss.getAverage(); long count = dss.getCount(); double max = dss.getMax();

  • String joining():连接流中每个字符串。比如把所有人的名字提取出来,在通过"-"横杠拼接起来,例子:String str= list.stream().map(Employee::getName).collect(Collectors.joining("-"));

  • Optional<T> maxBy():根据比较器选择最大值,比如求最高工资, 例子:Optional<Double> collect1 = list.stream().map(Employee::getSalary).collect(Collectors.maxBy((x, y) -> (int) (x - y)));

  • Optional<T> minBy():根据比较器选择最小值,比如求最低工资, 例子:Optional<Double> collect1 = list.stream().map(Employee::getSalary).collect(Collectors.minBy((x, y) -> (int) (x - y)));

  • 归约产生的类型 reducing():从一个作为累加器的初始值开始,利用BinaryOperator与流中元素逐个结合,从而归约成单个值 // 注意:把工资要提取出来,在进行 reducing() 例子:Double collect = list.stream().map(Employee::getSalary).collect(Collectors.reducing(0.0, (x, y) -> x + y));

  • 转换函数返回的类型 collectingAndThen():包裹另一个收集器,对其结果转换函数。 例子:int how= list.stream().collect(Collectors.collectingAndThen(Collectors.toList(), List::size));

  • Map<K, List<T>> groupingBy():根据某属性值对流分组,属性为K,结果为V。比如按照状态分组: 例子:Map<Emp.Status, List<Emp>> map= list.stream().collect(Collectors.groupingBy(Employee::getStatus));

  • Map<Boolean, List<T>> partitioningBy():根据true或false进行分区 。比如工资大于等于6000的一个区、小于6000的一个区: 例子:Map<Boolean,List<Emp>> vd= list.stream().collect(Collectors.partitioningBy(Employee::getSalary));

测试1:

 public class MyTest5 {
     public static void main(String[] args) {
         List<Employee> list = Arrays.asList(
                 new Employee(102, "李四", 59, 6666.66),
                 new Employee(101, "张三", 18, 9999.99),
                 new Employee(103, "王五", 28, 3333.33),
                 new Employee(104, "赵六", 8, 7777.77),
                 new Employee(104, "赵六", 8, 7777.77),
                 new Employee(104, "赵六", 8, 7777.77),
                 new Employee(105, "田七", 38, 5555.55)
         );
  
         DoubleSummaryStatistics collect = list.stream().collect(Collectors.summarizingDouble(Employee::getSalary));
         //System.out.println(collect);
         double average = collect.getAverage();
         double max = collect.getMax();
         double min = collect.getMin();
         double sum = collect.getSum();
         long count = collect.getCount();
  
         System.out.println(average);
         System.out.println(max);
         System.out.println(sum);
         System.out.println(min);
         System.out.println(count);
  
     }
 }

测试2:

 public class MyTest6 {
     public static void main(String[] args) {
         // String joining () 连接流中每个字符串 比如把所有人的名字提取出来, 在通过 "-" 横杠拼接起来
         // 例子:
         // String str = list.stream().map(Employee::getName).collect(Collectors.joining("-"));
  
         List<Employee> list = Arrays.asList(
                 new Employee(102, "李四", 59, 6666.66),
                 new Employee(101, "张三", 18, 9999.99),
                 new Employee(103, "王五", 28, 3333.33),
                 new Employee(104, "赵六", 8, 7777.77),
                 new Employee(104, "赵六", 8, 7777.77),
                 new Employee(104, "赵六", 8, 7777.77),
                 new Employee(105, "田七", 38, 5555.55)
         );
  
         String collect = list.stream().map(Employee::getName).collect(Collectors.joining("-", "【", "】"));
         System.out.println(collect);
     }
 }

4. 并行流与串行流

并行流就是把一个内容分成多个数据块,并用不同的线程分别处理每个数据块的流。

Java 8 中将并行进行了优化,我们可以很容易的对数据进行并行操作。

Stream API 可以声明性地通过 parallel() 与sequential() 在并行流与顺序流之间进行切换。

5. 练习

5.1 foreach()

作用:遍历集合元素

举例说明:

 System.out.println("遍历集合元素方式1:java8之前");
 for (Integer integer : list) {
     System.out.println(integer);
 }
 ​
 System.out.println("遍历集合元素方式2:java8之前");
 for (int i = 0; i < list.size(); i++) {
     System.out.println(list.get(i));
 }
 ​
 System.out.println("遍历集合元素方式3:java8 lambda");
 list.forEach(e-> System.out.println(e));
 ​
 System.out.println("遍历集合元素方式4:java8 lambda");
 list.forEach(System.out::println);
 ​
 System.out.println("遍历集合元素方式5:java 8 stream lambda");
 list.stream().forEach(x-> System.out.println(x));

输出结果:

 遍历集合元素方式1:java8之前
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 遍历集合元素方式2:java8之前
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 遍历集合元素方式3:java8 lambda
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 遍历集合元素方式4:java8 lambda
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 遍历集合元素方式5:java 8 stream lambda
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9

5.2 sorted()

作用:返回由该流的元素组成的流,并根据提供的 Comparator进行排序

举例说明:

 // 按照list中元素大小进行排序
 System.out.println("测试sorted(): 正序");
 list.stream().sorted(Comparator.comparing(Integer::intValue)).forEach(System.out::println);
 ​
 System.out.println("测试sorted(): 倒序");
 list.stream().sorted(Comparator.comparing(Integer::intValue).reversed()).forEach(System.out::println);

输出结果:

 测试sorted(): 正序
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 测试sorted(): 倒序
 9
 8
 7
 6
 5
 4
 3
 2
 1
 0

5.3 filter()

作用:返回满足指定条件的元素组成的流

举例说明:

 System.out.println("测试filter():java8之前");
 for (Integer integer : list) {
     if(integer%2==0){
         System.out.println(integer);
     }
 }
 ​
 System.out.println("测试filter():java8 stream");
 list.stream().filter(e->e%2==0).forEach(System.out::println);

输出结果:

 测试filter():java8之前
 0
 2
 4
 6
 8
 测试filter():java8 stream
 0
 2
 4
 6
 8

5.4 limit()

作用:返回由该流的元素组成的流,截断长度不能超过maxSize。

举例说明:

 System.out.println("测试limit()");
 list.stream().limit(5).forEach(System.out::println);

输出结果:

 测试limit()
 0
 1
 2
 3
 4

5.5 skip()

作用:在丢弃流的前n个元素后,返回由该流其余元素组成的流。如果此流包含少于n元素,那么将返回一个空流。

举例说明:

 System.out.println("测试skip()");
 list.stream().skip(5).forEach(System.out::println);
 list.stream().skip(15).forEach(System.out::println);

输出结果:

 测试skip()
 5
 6
 7
 8
 9

5.6 distinct()

作用:返回由该流的不同元素(根据Object.equals(Object))组成的流。

举例说明:

 List<Integer> newList = new ArrayList();
 newList.add(1);
 newList.add(2);
 newList.add(3);
 newList.add(1);
 newList.add(3);
 System.out.println("集合newList元素");
 newList.forEach(System.out::println);
 System.out.println("测试distinct");
 newList.stream().distinct().forEach(System.out::println);

输出结果:

 集合newList元素
 1
 2
 3
 1
 3
 测试distinct
 1
 2
 3

5.7 map()

作用:返回由给定函数应用于此流的元素的结果组成的流。

举例说明:

 System.out.println("测试map()");
 newList.stream().map(e->e%2==0).forEach(System.out::println);
 newList.stream().map(e->e/2).forEach(System.out::println);

输出结果:

 测试map()
 false
 true
 false
 false
 false
 0
 1
 1
 0
 1

5.8 findFirst()

作用:返回此流的第一个元素的Optional,如果流为空,则返回一个空的Optional。 如果流没有遇到顺序,则可能会返回任何元素。

举例说明:

 System.out.println("测试findFirst");
 Integer integer = newList.stream().findFirst().get();
 System.out.println("integer = " + integer);
 ​
 System.out.println("测试filet+findFirst");
 Integer integer1 = newList.stream().filter(e -> e % 2 == 0).findFirst().get();
 System.out.println("integer1 = " + integer1);

输出结果:

 测试findFirst
 integer = 1
 测试filet+findFirst
 integer1 = 2

5.9 reduce()

作用:根据指定的计算模型将Stream中的值计算得到一个最终结果

举例说明:

 System.out.println("测试reduce()");
 Integer reduceMax = newList.stream().reduce((e1, e2) -> e1.intValue() > e2.intValue() ? e1 : e2).get();
 System.out.println("reduceMax = " + reduceMax);
 Integer reduceMin = newList.stream().reduce((e1, e2) -> e1.intValue() < e2.intValue() ? e1 : e2).get();
 System.out.println("reduceMin = " + reduceMin);

输出结果:

 测试reduce()
 reduceMax = 3
 reduceMin = 1

5.10 max()、min()

作用:计算最大值、最小值

举例说明:

 ystem.out.println("测试max、min");
 Integer max = newList.stream().max(Comparator.comparing(Integer::intValue)).get();
 System.out.println("max = " + max);
 Integer min = newList.stream().min(Comparator.comparing(Integer::intValue)).get();
 System.out.println("min = " + min);

输出结果:

 测试max、min
 max = 3
 min = 1

5.11 groupingBy()

作用:分组

举例说明:

 System.out.println("测试groupingBy()");
 Map<Integer, List<Integer>> collect = newList.stream().collect(Collectors.groupingBy(Integer::intValue));
 collect.keySet().forEach(System.out::println);
 collect.values().forEach(System.out::println);

输出结果:

 测试groupingBy()
 1
 2
 3
 [1, 1]
 [2]
 [3, 3]

5.12 toMap()

作用:从list中指定key、value,转为map格式数据

举例说明:

 // 转map,key为id,value为对象
 list.stream().collect(Collectors.toMap(LogsDO::getId, Function.identity()))
 ​
 // 转map,key为id,value为请求方法
 list.stream().collect(Collectors.toMap(LogsDO::getId, LogsDO::getActionMethod));

5.13 数值流

mapToInt():返回一个IntStream ,其中包含将给定函数应用于此流的元素的结果。

 IntStream intStream = list.stream().mapToInt(LogsDO::getId);

mapToLong():返回一个LongStream ,其中包含将给定函数应用于此流的元素的结果。

 LongStream longStream = list.stream().mapToLong(LogsDO::getConsumingTime);

mapToDouble():返回一个DoubleStream ,其中包含将给定函数应用于此流的元素的结果。

方法

 System.out.println(list.stream().mapToLong(LogsDO::getConsumingTime).sum());
 System.out.println(list.stream().mapToLong(LogsDO::getConsumingTime).max().getAsLong());
 System.out.println(list.stream().mapToLong(LogsDO::getConsumingTime).min().getAsLong());
 System.out.println(list.stream().mapToLong(LogsDO::getConsumingTime).average().getAsDouble());
 System.out.println(list.stream().mapToLong(LogsDO::getConsumingTime).count());

5.14 其它

 package streamAPI;
 ​
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 import java.util.stream.Collectors;
 ​
 /**
  * @ClassName Test01
  * @Description TODO
  * @Author Jiangnan Cui
  * @Date 2022/9/18 15:48
  * @Version 1.0
  */
 public class Test01 {
     public static void main(String[] args) {
         // 创建list
         List<Integer> list = new ArrayList<>();
         // 添加10个元素
         for (int i = 0; i < 10; i++) {
             list.add(i);
         }
         System.out.println("遍历list元素");
         list.forEach(e-> System.out.println(e));
 ​
         System.out.println("测试map:所有元素*2,保存为一个新的list");
         List<Integer> collect = list.stream().map(n -> n * 2).collect(Collectors.toList());
         collect.forEach(System.out::println);
 ​
         System.out.println("测试filer:取出所有偶数,并保存为一个新的集合");
         List<Integer> list1 = list.stream().filter(n -> n % 2 == 0).collect(Collectors.toList());
         list1.forEach(System.out::println);
 ​
         System.out.println("测试distinct:去重");
         List<Integer> list2 = list.stream().distinct().collect(Collectors.toList());
         list2.forEach(System.out::println);
 ​
         System.out.println("测试skip:跳过前3个元素开始取");
         List<Integer> list3 = list.stream().skip(3).collect(Collectors.toList());
         list3.forEach(System.out::println);
 ​
         System.out.println("测试limit:取前3个元素");
         Set<Integer> set = list.stream().limit(3).collect(Collectors.toSet());
         set.forEach(System.out::println);
 ​
         System.out.println("测试:skip + limit");
         List<Integer> list4 = list.stream().skip(3).limit(5).collect(Collectors.toList());
         list4.forEach(System.out::println);
     }
 }

输出结果:

 遍历list元素
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 测试map:所有元素*2,保存为一个新的list
 0
 2
 4
 6
 8
 10
 12
 14
 16
 18
 测试filer:取出所有偶数,并保存为一个新的集合
 0
 2
 4
 6
 8
 测试distinct:去重
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 测试skip:跳过前3个元素开始取
 3
 4
 5
 6
 7
 8
 9
 测试limit:取前3个元素
 0
 1
 2
 测试:skip + limit
 3
 4
 5
 6
 7
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值