浅析Java8新特性-Stream流操作:Stream概念、常见中间/终止操作符、创建stream的3种方式、串行流/并行流的区分、使用示例(遍历/匹配、过滤、聚合、映射、归约、归集、统计、分区分组、接合、排序、组合/提取、分页、并行、集合转Map、使用并行流注意点)

摘要:
流使用一种类似于使用SQL语句从数据库查询数据的直观方式,提供Java集合操作和表达式的高级抽象。流是来自数据源的元素队列,支持聚合操作。元素是特定类型的对象。集合接口有两种方法来生成流:filter方法用于通过设置的条件过滤出元素。以下代码片段使用limit方法打印10条数据Randomrandom=newRandom();

一、Java 8 Stream 介绍

  Java8 API 添加了一个新的抽象称为 流Stream,可以让你以一种声明的方式处理数据。Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达的高阶抽象。

  Stream 这种风格将要处理的元素集合看作一种流,在流的过程中,流在管道中传输, 并且可以在管道的节点上进行处理,借助 Stream API 对流中的元素进行操作,比如:筛选、排序、聚合等。

浅析Java8新特性-Stream流操作:Stream概念、常见中间/终止操作符、创建stream的3种方式、串行流/并行流的区分、使用示例(遍历/匹配、过滤、聚合、映射、归约、归集、统计、分区分组、接合、排序、组合/提取、分页、并行、集合转Map、使用并行流注意点)第1张

  元素流在管道中经过中间操作(intermediate operation)的处理,最后由最终操作(terminal operation)得到前面处理的结果。

1、什么是 Stream:Stream(流)是一个来自数据源的元素队列并支持聚合操作

  元素是特定类型的对象,形成一个队列。 Java中的Stream并不会存储元素,而是按需计算。

  数据源: 流的来源,可以是集合,数组,I/O channel, 产生器generator 等。

  聚合操作:类似SQL语句一样的操作, 比如 filter, map, reduce, find, match, sorted 等。

  和以前的Collection操作不同, Stream操作还有两个基础的特征:

(1)Pipelining: 中间操作都会返回流对象本身。 这样多个操作可以串联成一个管道, 如同流式风格(fluent style)。 这样做可以对操作进行优化, 比如延迟执行(laziness)和短路( short-circuiting)。

(2)内部迭代: 以前对集合遍历都是通过Iterator或者For-Each的方式, 显式的在集合外部进行迭代, 这叫做外部迭代。 Stream提供了内部迭代的方式, 通过访问者模式(Visitor)实现。

2、生成流:在 Java 8 中,集合接口有两个方法来生成流:

(1)stream() − 为集合创建串行流。

(2)parallelStream() − 为集合创建并行流。

3、常用方法

forEach:Stream 提供了新的方法 'forEach' 来迭代流中的每个数据。

map 方法用于映射每个元素到对应的结果。

filter 方法用于通过设置的条件过滤出元素。

limit 方法用于获取指定数量的流。 以下代码片段使用 limit 方法打印出 10 条数据

Random random = new Random();
random.ints().limit(10).forEach(System.out::println);

sorted 方法用于对流进行排序

二、操作符

1、什么是操作符呢?

  操作符就是对数据进行的一种处理工作,一道加工程序;就好像工厂的工人对流水线上的产品进行一道加工程序一样。Stream的操作符大体上分为两种:中间操作符终止操作符

2、中间操作符  ——  对于数据流来说,中间操作符在执行制定处理程序后,数据流依然可以传递给下一级的操作符。中间操作符包含8种(排除了parallel,sequential,这两个操作并不涉及到对数据流的加工操作):

(1)map(mapToInt,mapToLong,mapToDouble) 转换操作符,把比如A->B,这里默认提供了转int,long,double的操作符。

(2)flatmap(flatmapToInt,flatmapToLong,flatmapToDouble) 拍平操作比如把 int[]{2,3,4} 拍平 变成 2,3,4 也就是从原来的一个数据变成了3个数据,这里默认提供了拍平成int,long,double的操作符。

(3)limit 限流操作,比如数据流中有10个 我只要出前3个就可以使用。

(4)distint 去重操作,对重复元素去重,底层使用了equals方法。

(5)filter 过滤操作,把不想要的数据过滤。

(6)peek 挑出操作,如果想对数据进行某些操作,如:读取、编辑修改等。

(7)skip 跳过操作,跳过某些元素。

(8)sorted(unordered) 排序操作,对元素排序,前提是实现Comparable接口,当然也可以自定义比较器。

3、终止操作符  ——  数据经过中间加工操作,就轮到终止操作符上场了;

  终止操作符就是用来对数据进行收集或者消费的,数据到了终止操作这里就不会向下流动了,终止操作符只能使用一次。

(1)collect 收集操作,将所有数据收集起来,这个操作非常重要,官方的提供的Collectors 提供了非常多收集器,可以说Stream 的核心在于Collectors。

(2)count 统计操作,统计最终的数据个数。

(3)findFirst、findAny 查找操作,查找第一个、查找任何一个 返回的类型为Optional。

(4)noneMatch、allMatch、anyMatch 匹配操作,数据流中是否存在符合条件的元素 返回值为bool 值。

(5)min、max 最值操作,需要自定义比较器,返回数据流中最大最小的值。

(6)reduce 规约操作,将整个数据流的值规约为一个值,count、min、max底层就是使用reduce。

(7)forEach、forEachOrdered 遍历操作,这里就是对最终的数据进行消费了。

(8)toArray 数组操作,将数据流的元素转换成数组。

4、代码示例  ——  Stream 的一系列操作必须要使用终止操作,否者整个数据流是不会流动起来的,即处理操作不会执行。

  总体来说使用并不陌生,跟数组类的使用挺像的。具体代码示例可以看这篇文章:https://www.jianshu.com/p/11c925cdba50

三、stream 的创建

1、创建流 stream 的方式

// 1、通过 java.util.Collection.stream() 方法用集合创建流
List<String> list = Arrays.asList("a", "b", "c");
// 创建一个顺序流
Stream<String> stream = list.stream();
// 创建一个并行流
Stream<String> parallelStream = list.parallelStream();

// 2、使用java.util.Arrays.stream(T[] array)方法用数组创建流
int[] array={1,3,5,6,8};
IntStream stream = Arrays.stream(array);

// 3、使用Stream的静态方法:of()、iterate()、generate()
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);

Stream<Integer> stream2 = Stream.iterate(0, (x) -> x + 3).limit(4);
stream2.forEach(System.out::println); // 0 3 6 9

Stream<Double> stream3 = Stream.generate(Math::random).limit(3);
stream3.forEach(System.out::println);

2、stream 和 parallelStream 的简单区分

  stream 是顺序流,由主线程按顺序对流执行操作; parallelStream 是并行流,内部以多线程并行执行的方式对流进行操作,但前提是流中的数据处理没有顺序要求。例如筛选集合中的奇数,两者的处理不同之处

浅析Java8新特性-Stream流操作:Stream概念、常见中间/终止操作符、创建stream的3种方式、串行流/并行流的区分、使用示例(遍历/匹配、过滤、聚合、映射、归约、归集、统计、分区分组、接合、排序、组合/提取、分页、并行、集合转Map、使用并行流注意点)第2张

四、Stream 使用代码示例

1、遍历/匹配(forEach / find / match)

  Stream 也是支持类似集合的遍历和匹配元素的,只是Stream中的元素是以Optional类型存在的。Stream的遍历、匹配非常简单。

List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
// 遍历输出符合条件的元素:先用 filter 过滤,再用 forEach 遍历(跟数组用法挺像)
list.stream().filter(x -> x > 6).forEach(System.out::println);
// 匹配第一个
Optional<Integer> findFirst = list.stream().filter(x -> x > 6).findFirst();
// 匹配任意(适用于并行流)
Optional<Integer> findAny = list.parallelStream().filter(x -> x > 6).findAny();
// 是否包含符合特定条件的元素
boolean anyMatch = list.stream().anyMatch(x -> x < 6);

2、筛选(filter):筛选,是按照一定的规则校验流中的元素,将符合条件的元素提取到新的流中的操作。

3、聚合(max/min/count)

  max、min、count这些字眼你一定不陌生,没错,在mysql中我们常用它们进行数据统计。Java stream中也引入了这些概念和用法,极大地方便了我们对集合、数组的数据统计工作

(1)获取String集合中最长的元素

public class StreamTest {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
        Optional<String> max = list.stream().max(Comparator.comparing(String::length));
        System.out.println("最长的字符串:" + max.get());
    }
}

(2)计算Integer集合中大于6的元素的个数

public class StreamTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(7, 6, 4, 8, 2, 11, 9);
        long count = list.stream().filter(x -> x > 6).count();
        System.out.println("list中大于6的元素个数:" + count);
    }
}

4、映射(map/flatMap):映射可以将一个流的元素按照一定的映射规则映射到另一个流中。分为map和flatMap:

  • map:接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。
  • flatMap:接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流
(1)英文字符串数组的元素全部改为大写。整数数组每个元素+3
public class StreamTest {
    public static void main(String[] args) {
        String[] strArr = { "abcd", "bcdd", "defde", "fTr" };
        List<String> strList = Arrays.stream(strArr).map(String::toUpperCase).collect(Collectors.toList());
        System.out.println("每个元素大写:" + strList);
        List<Integer> intList = Arrays.asList(1, 3, 5, 7, 9, 11);
        List<Integer> intListNew = intList.stream().map(x -> x + 3).collect(Collectors.toList());
        System.out.println("每个元素+3:" + intListNew);
    }
}

(2)将两个字符数组合并成一个新的字符数组。

public class StreamTest {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("m,k,l,a", "1,3,5,7");
        List<String> listNew = list.stream().flatMap(s -> {
            // 将每个元素转换成一个stream
            String[] split = s.split(",");
            Stream<String> s2 = Arrays.stream(split);
            return s2;
        }).collect(Collectors.toList());
        System.out.println("处理前的集合:" + list);
        System.out.println("处理后的集合:" + listNew);
    }
}

5、归约(reduce):归约,也称缩减,顾名思义,是把一个流缩减成一个值,能实现对集合求和、求乘积和求最值操作。

List<Integer> list = Arrays.asList(1, 3, 2, 8, 11, 4);
// 求和方式
Optional<Integer> sum = list.stream().reduce(Integer::sum);
Integer sum3 = list.stream().reduce(0, Integer::sum);
// 求乘积 Optional<Integer> product = list.stream().reduce((x, y) -> x * y);
// 求最大值方式 Optional<Integer> max = list.stream().reduce((x, y) -> x > y ? x : y); Integer max2 = list.stream().reduce(1, Integer::max);

6、归集(toList/toSet/toMap):因为流不存储数据,那么在流中的数据完成处理后,需要将流中的数据重新归集到新的集合里。toList、toSet和toMap比较常用,另外还有toCollection、toConcurrentMap等复杂一些的用法。

List<Integer> list = Arrays.asList(1, 6, 3, 4, 6, 7, 9, 6, 20);
List
<Integer> listNew = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toList()); Set<Integer> set = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toSet());
List
<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 23, "male", "New York")); personList.add(new Person("Jack", 7000, 25, "male", "Washington")); Map<?, Person> map = personList.stream().filter(p -> p.getSalary() > 8000) .collect(Collectors.toMap(Person::getName, p -> p));

7、统计(count/averaging):Collectors提供了一系列用于数据统计的静态方法:

  • 计数:count
  • 平均值:averagingInt、averagingLong、averagingDouble
  • 最值:maxBy、minBy
  • 求和:summingInt、summingLong、summingDouble
  • 统计以上所有:summarizingInt、summarizingLong、summarizingDouble

8、分组(partitioningBy/groupingBy)

  • 分区:将stream按条件分为两个Map,比如员工按薪资是否高于8000分为两部分。
  • 分组:将集合分为多个Map,比如员工按性别分组。有单级分组和多级分组。
  案例:将员工按薪资是否高于8000分为两部分;将员工按性别和地区分组
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900, 23, "male", "Washington"));
personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
personList.add(new Person("Lily", 7800, 21, "female", "New York"));
personList.add(new Person("Anni", 8200, 24, "female", "New York"));
// 将员工按薪资是否高于8000分组
Map<Boolean, List<Person>> part = personList.stream()
  .collect(Collectors.partitioningBy(x -> x.getSalary() > 8000)); // 将员工按性别分组 Map<String, List<Person>> group = personList.stream()
  .collect(Collectors.groupingBy(Person::getSex));
// 将员工先按性别分组,再按地区分组 Map<String, Map<String, List<Person>>> group2 = personList.stream()
  .collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea)));
// 结果
// 员工按薪资是否大于8000分组情况:
{
false=[
  Person{name='Jack', salary=7000, age=25, sex='male', area='Washington'}, 
  Person{name='Lily', salary=7800, age=21, sex='female', area='New York'}
],
true=[
  Person{name='Tom', salary=8900, age=23, sex='male', area='Washington'},
  Person{name='Anni', salary=8200, age=24, sex='female', area='New York'}
]
}
// 员工按性别分组情况: {

female=[
  Person{name='Lily', salary=7800, age=21, sex='female', area='New York'},
  Person{name='Anni', salary=8200, age=24, sex='female', area='New York'}
],
male=[
  Person{name='Tom', salary=8900, age=23, sex='male', area='Washington'},
  Person{name='Jack', salary=7000, age=25, sex='male', area='Washington'}
]
}
// 员工按性别、地区: {

  female={
    New York=[
      Person{name='Lily', salary=7800, age=21, sex='female', area='New York'},
      Person{name='Anni', salary=8200, age=24, sex='female', area='New York'}
    ]
  },
  male={
    Washington=[
      Person{name='Tom', salary=8900, age=23, sex='male', area='Washington'},
      Person{name='Jack', salary=7000, age=25, sex='male', area='Washington'}
    ]
  }
}

9、接合(joining):joining可以将stream中的元素用特定的连接符(没有的话,则直接连接)连接成一个字符串。

10、排序(sorted):sorted,中间操作。有两种排序:
  • sorted():自然排序,流中元素需实现Comparable接口
  • sorted(Comparator com):Comparator排序器自定义排序
  案例:将员工按工资由高到低(工资一样则按年龄由大到小)排序
List<Person> personList = new ArrayList<Person>();// 按工资升序排序(自然排序)
List<String> newList = personList.stream().sorted(Comparator.comparing(Person::getSalary)).map(Person::getName)
                .collect(Collectors.toList());
// 按工资倒序排序
List<String> newList2 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed())
                .map(Person::getName).collect(Collectors.toList());
// 先按工资再按年龄升序排序
List<String> newList3 = personList.stream()
                .sorted(Comparator.comparing(Person::getSalary).thenComparing(Person::getAge)).map(Person::getName)
                .collect(Collectors.toList());
// 先按工资再按年龄自定义排序(降序)- 自定义排序
List<String> newList4 = personList.stream().sorted((p1, p2) -> {
            if (p1.getSalary() == p2.getSalary()) {
                return p2.getAge() - p1.getAge();
            } else {
                return p2.getSalary() - p1.getSalary();
            }
        }).map(Person::getName).collect(Collectors.toList());

11、提取/组合:流也可以进行合并、去重、限制、跳过等操作。

String[] arr1 = { "a", "b", "c", "d" };
String[] arr2 = { "d", "e", "f", "g" };
Stream<String> stream1 = Stream.of(arr1);
Stream<String> stream2 = Stream.of(arr2);
// concat:合并两个流 distinct:去重
List<String> newList = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList());
// limit:限制从流中获得前n个数据
List<Integer> collect = Stream.iterate(1, x -> x + 2).limit(10).collect(Collectors.toList());
// skip:跳过前n个数据
List<Integer> collect2 = Stream.iterate(1, x -> x + 2).skip(1).limit(5).collect(Collectors.toList());

12、分页操作:stream api 的强大之处还不仅仅是对集合进行各种组合操作,还支持分页操作。

  例如,将如下的数组从小到大进行排序,排序完成之后,从第1行开始,查询10条数据出来,操作如下:

//需要查询的数据
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5, 10, 6, 20, 30, 40, 50, 60, 100);
List<Integer> dataList = numbers.stream().sorted(Integer::compareTo).skip(0).limit(10).collect(Collectors.toList());
System.out.println(dataList.toString());

13、并行操作

  所谓并行,指的是多个任务在同一时间点发生,并由不同的cpu进行处理,不互相抢占资源;而并发,指的是多个任务在同一时间点内同时发生了,但由同一个cpu进行处理,互相抢占资源

  stream api 的并行操作和串行操作,只有一个方法区别,其他都一样,例如下面我们使用parallelStream来输出空字符串的数量:

List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
// 采用并行计算方法,获取空字符串的数量
long count = strings.parallelStream().filter(String::isEmpty).count();
System.out.println(count);

  注意:在实际使用的时候,并行操作不一定比串行操作快!对于简单操作,数量非常大,同时服务器是多核的话,建议使用Stream并行!反之,采用串行操作更可靠!

14、集合转Map操作

  在实际的开发过程中,还有一个使用最频繁的操作就是,将集合元素中某个主键字段作为key,元素作为value,来实现集合转map的需求,这种需求在数据组装方面使用的非常多。

public static void main(String[] args) {
    List<Person> personList = new ArrayList<>();
    personList.add(new Person("Tom",7000,25,"male","安徽"));
    personList.add(new Person("Jack",8000,30,"female","北京"));
    personList.add(new Person("Lucy",9000,40,"male","上海"));
    personList.add(new Person("Airs",10000,40,"female","深圳"));
    Map<Integer, Person> collect = personList.stream().collect(Collectors.toMap(Person::getAge, v -> v, (k1, k2) -> k1));
    System.out.println(collect);
}
{
  40=Person{name='Lucy', salary=9000, age=40, sex='male', area='上海'}, 
  25=Person{name='Tom', salary=7000, age=25, sex='male', area='安徽'},
  30=Person{name='Jack', salary=8000, age=30, sex='female', area='北京'} }

  打开Collectors.toMap方法源码,一起来看看。

public static <T, K, U>
    Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
                                    Function<? super T, ? extends U> valueMapper,
                                    BinaryOperator<U> mergeFunction) {
    return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
}

  从参数表可以看出:

  • 第一个参数:表示 key
  • 第二个参数:表示 value
  • 第三个参数:表示某种规则

  上文中的Collectors.toMap(Person::getAge, v -> v, (k1,k2) -> k1),表达的意思就是将age的内容作为key,v -> v是表示将元素person作为value,其中(k1,k2) -> k1表示如果存在相同的key,将第一个匹配的元素作为内容,第二个舍弃!

15、特别注意点:

  使用并行流 parallelStream,千万不能使用 toMap 方法,toMap 使用的是 HashMap,得用 toConcurrentMap

//错误示例
Map<String, User> userMap = userList.parallelStream()
  .collect(Collectors.toMap(User::getClassName, Function.identity()));
//正确示例 Map<String, User> userMap = userList.parallelStream()

  .collect(Collectors.toConcurrentMap(User::getClassName, Function.identity()));

  也不能使用ArrayList,得用 Vector

List<String> usernames = new ArrayList<>();
//返回结果顺序不变
userList.forEach(user -> {
  usernames.add(user.getUsername());
});
//或者,返回结果顺序不变,虽然用的并行流
List<String> collect1 = userList.parallelStream().map(User::getUsername).collect(Collectors.toList());
//返回结果顺序改变
userList.parallelStream().forEach(user -> {
  usernames.add(user.getUsername());
});

免责声明:文章转载自《浅析Java8新特性-Stream流操作:Stream概念、常见中间/终止操作符、创建stream的3种方式、串行流/并行流的区分、使用示例(遍历/匹配、过滤、聚合、映射、归约、归集、统计、分区分组、接合、排序、组合/提取、分页、并行、集合转Map、使用并行流注意点)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Makefile学习(三)执行make流量取证-流量中提取文件下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C# dictionary

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Demo1 { class Program { static void...

离群值检测

离群值检测 离群值 outlier:样本中的一个或几个观测值,它们离其他观测值较远,暗示它们可能来自不同的总体。 离群值分类 总体固有变异性的极端表现,这类离群值与样本的其余观测值属于统一总体; 由于试验条件和试验方法的偶然偏离所产生的结果,或产生与观察、记录、计算中的失误,这类离群值与样本中其余观测值不属于统一总体。 数学小知识 方差: 标准差: ​...

Java生成excel导出文件(使用poi+JXL)

1.使用poi生成文件 package com.mi.entity; import java.util.Date; public class Student { private int id; private String name; private int age; private Date birth;...

利用Matlab自带的深度学习工具进行车辆区域检测与车型识别【福利-内附源码与数据库】(一)

前言 本此的博客详细记录了我使用Matlab进行车辆区域检测(R-CNN)与车型识别(AlexNet)的过程。并且内包含了训练数据集、测试数据集以及源码。 训练数据集是使用的斯坦福大学的一个车型数据库,内含196种不同的车型。写到这里我真的很想吐槽一下这个数据库里面的奥迪车系:很多黑白的图片啊喂!!! 做训练的时候AlexNet数据输入维度是3啊喂!!!...

CSS之Normalize.css的使用(重置表)

本文译自Normalize.css官网: http://nicolasgallagher.com/about-normalize-css/ Normalize.css 只是一个很小的CSS文件,但它在默认的HTML元素样式上提供了跨浏览器的高度一致性。相比于传统的CSS reset,Normalize.css是一种现代的、为HTML5准备的优质替代方案。N...

jQuery----1

简介   jQuery可以像CSS一样选择页面内的元素。比如:$('p')会选中所有的段落。   使用$(document).ready()就可以添加文档载入完毕之后执行的代码。   使用$.fn.func = function(){}的方式可以扩展jQuery。   选择器   简单示例:   <script type="text/JavaSc...