Stream简化元素计算;1、接口设计Stream流的概念从Java1.8开始提出,着重于对源数据计算能力的封装,支持串行和并行两种运行方式;还是先看核心接口的设计:BaseStream:Basic接口,声明了流管理的核心方法;Stream:核心接口,声明了流操作的核心方法,其他接口为指定类型的适配;基本情况:通过指定元素的值返回一个序列流,元素的内容为字符串并转为Long类型,最后计算求和结果并返回;System.out.println("sum1="+IntStream.of(1,2,3).sum());System.out.println("sum2="+Stream.of("1","2","3").mapToLong(Long::parseLong).sum());整个Stream处理过程可以分为三段:创建流、中间操作、最终操作,即多个元素值通过流计算最终得到求和结果;2、创建操作除了Stream提供的创建方法外,在Java1.8中,执行了很多容器类的方法。该扩展提供了传输集合元素的能力;Stream创建StreamintStream=Stream.of(1,2);Collection创建ListgetList=Arrays.asList("hello","copy");StreamstrStream=getList.stream();Array创建Double[]getArray=newDouble[]{1.1,2.2};StreamdouStream=Arrays.stream(getArray);上面方法创建的Stream默认是串行序列,可以通过Stream.isParallel判断;Stream.parallel方法的执行可以转化为并行流;3.中间操作通常可以看作是对Stream中间操作的源查询,属于惰性设计。仅在需要时才对源数据执行计算被执行,类似于数据库中视图的原理;Stream流的强大之处在于它提供了丰富的中间操作。与集合或数组等容器相比,大大简化了源数据的计算复杂度。案例中的数据结构如下;公共类TesStream{publicstaticvoidmain(String[]args){ListuserList=getUserList();}privatestaticListgetUserList(){ListuserList=newArrayList<>();userList.add(newUser(1,"张三","上海"));userList.add(newUser(2,"李四","北京"));userList.add(newUser(3,"王舞","北京"));userList.add(newUser(4,"顺六","上海,杭州"));返回用户列表;}}filter:过滤,输出id大于1的用户;userList.stream().filter(user->user.getId()>1).forEach(System.out::println);map:将已有元素转换映射为对应的结果,并输出用户所在的城市;userList.stream().map(user->user.getName()+"in"+user.getCity()).forEach(System.out::println);peek:遍历元素,每个用户id加1个输出;userList.stream().peek(用户->user.setId(user.getId()+1)).forEach(System.out::println);flatMap:数据拆分一对多映射,用户分布在多个城市;userList.stream().flatMap(user->Arrays.stream(user.getCity().split(","))).forEach(System.out::println);sorted:指定属性排序,倒序输出根据用户ID;userList.stream().sorted(Comparator.comparingInt(User::getId).reversed()).forEach(System.out::println);distinct:去重,用户所在城市去重后输出;userList.stream().map(User::getCity).distinct().forEach(System.out::println);skip&limit:拦截,跳过过滤后的数据,拦截第一项;userList.stream().filter(user->user.getId()>1).skip(1).limit(1).forEach(System.out::println);相比Java1.8之前的集合、数组的处理逻辑,通过Stream方法简化了数据修改、查询、过滤、排序等一系列操作。以上仅涉及每次遍历的final方法;4.FinaloperationStream流执行完final操作后,不能再做其他动作,否则会报status异常,表示stream已经执行完毕或关闭。如果要再次执行操作,必须重新创建Stream流;min:最小值,获取用户的最小id值;intmin=userList.stream().min(Comparator.comparingInt(User::getId)).get().getId();max:最大值,获取用户id值的最大值;intmax=userList.stream().max(Comparator.comparingInt(User::getId)).get().getId();sum:sum,用户ID的累计和;intsum=userList.stream().mapToInt(User::getId).sum();count:总数,id小于2的用户总数;longcount=userList.stream().filter(user->user.getId()<2).count();foreach:遍历,输出北京相关用户;userList.stream().filter(user->"北京".equals(user.getCity())).forEach(System.out::println);findAny:查找任意一个满足条件的元素,得到一个北京用户;UsergetUser=userList.stream().filter(user->"北京".equals(user.getCity())).findAny().get();findFirst:获取第一个满足条件的元素;UsergetUser=userList.stream().filter(user->"北京".equals(user.getCity())).findFirst().get();anyMatch:匹配判断,判断是否有深圳用户;booleanmatchFlag=userList.stream().anyMatch(user->"深圳".equals(user.getCity()));allMatch:全部匹配,判断所有用户的城市不为空;booleanmatchFlag=userList.stream().allMatch(user->StrUtil.isNotEmpty(user.getCity()));noneMatch:完全不匹配,判断没有用户的城市为空;booleanmatchFlag=userList.stream().noneMatch(user->StrUtil.isEmpty(user.getCity())。采集策略的核心接口,带指定将元素累积存储到结果容器中的能力;并提供Collectors工具中Collector接口的实现类;toList:将用户ID存放在List集合中;ListidList=userList.stream().map(User::getId).collect(Collectors.toList());toMap:将用户ID和Name以Key-Value的形式存储在Map集合中;MapuserMap=userList.stream().collect(Collectors.toMap(User::getId,User::getName));toSet:将用户的城市存储在Set集合中;SetcitySet=userList.stream().map(User::getCity).collect(Collectors.toSet());counting:符合条件的用户总数;longcount=userList.stream().filter(user->user.getId()>1).collect(Collectors.counting());summingInt:结果元素为UserID求和;整数sumInt=userList.stream().filter(user->user.getId()>2).collect(Collectors.summingInt(User::getId));minBy:过滤元素User中ID最小的用户将指定的分隔符转换成字符串;StringjoinCity=userList.stream().map(User::getCity).collect(Collectors.joining("||"));groupingBy:按条件分组,用户按城市分组;Map>groupCity=userList.stream().collect(Collectors.groupingBy(User::getCity));在代码工程中,会涉及到很多集合数据的计算逻辑,尤其是在微服务场景下,VO数据模型需要对多个服务的数据进行组装,通过Collector可以大大简化组装过程;$$结束$$