一、基本API初探

package java8.stream;

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream; /**
* @author jiaqing.xu@hand-china.com
* @version 1.0
* @name
* @description
* @date 2018/7/15
*/
public class BasicTest {
public static void main(String[] args) { //创建串行流
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
filtered.forEach(System.out::println); //使用foreach进行数据迭代 limit 方法用于获取指定数量的流
Random random = new Random();
random.ints().limit(10).forEach(System.out::println); //Map用于映射每个元素对应的结果,原值为i 映射到i*i .collect(Collectors.toList()):将stream再转换回list集合
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
// 获取对应的平方数
List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().limit(2).collect(Collectors.toList());
squaresList.forEach(System.out::println); //filter 方法用于通过设置的条件过滤出元素
List<String> stringList = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
// 获取空字符串的数量
Long count = stringList.stream().filter(string -> string.isEmpty()).count();
System.out.println("The count of empty string:"+count); //sorted用于对流进行排序,默认是从小到大
List<Integer> array = Arrays.asList(1,3,2,4);
List<Integer> sortedList = array.stream().sorted().collect(Collectors.toList());
sortedList.forEach(System.out::println); //流并行处理程序parallelStream
List<String> stringList2 = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl","");
// 获取空字符串的数量
Long count2 = stringList2.parallelStream().filter(string -> string.isEmpty()).count();
System.out.println("The count of empty string:"+count2); //collectors 可以返回列表或者字符串
List<String> stringList3 = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl","");
String mergedString = stringList3.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
System.out.println("合并字符串: " + mergedString); //
int[] numberList = {12,3,34,67,100,99}; IntStream intStream = IntStream.of(numberList);
IntSummaryStatistics stats = intStream.summaryStatistics(); System.out.println("列表中最大的数 : " + stats.getMax());
System.out.println("列表中最小的数 : " + stats.getMin());
System.out.println("所有数之和 : " + stats.getSum());
System.out.println("平均数 : " + stats.getAverage());
}
}

二、分组和合并

 /**
* @author jiaqing.xu@hand-china.com
* @version 1.0
* @name
* @description 分组、合并测试dto
* @date 2018/7/15
*/
public class Foo { private int code; private int count; public Foo(int code, int count) {
this.code = code;
this.count = count;
} public int getCode() {
return code;
} public void setCode(int code) {
this.code = code;
} public int getCount() {
return count;
} public void setCount(int count) {
this.count = count;
} @Override
public String toString() {
return "Foo{" +
"code=" + code +
", count=" + count +
'}';
}
}
/**
* @author jiaqing.xu@hand-china.com
* @version 1.0
* @name
* @description 测试分组和合并 groupingBy方法以及reduce方法
* @date 2018/7/15
*/
public class TestFoo {
public static void main(String[] args) {
Foo foo1 = new Foo(1, 2);
Foo foo2 = new Foo(2, 23);
Foo foo3 = new Foo(2, 6);
List<Foo> list = new ArrayList<>(4);
list.add(foo1);
list.add(foo2);
list.add(foo3);
Map<Integer, List<Foo>> collect = list.stream().collect(Collectors.groupingBy(Foo::getCode));
//存储最后的汇总结果集合
List<Foo> result = new ArrayList<>();
collect.forEach((k,v)->{
Optional<Foo> sum = v.stream().reduce(
(v1, v2) -> { //合并
v1.setCount(v1.getCount()+v2.getCount());
return v1;
}
);
result.add(sum.orElse(new Foo(0,10)));
});
result.forEach(System.out::print); }
}

将2号记录的count值进行了合并汇总!23+6=29

Java8新特性——stream流的更多相关文章

  1. 这可能是史上最好的 Java8 新特性 Stream 流教程

    本文翻译自 https://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/ 作者: @Winterbe 欢迎关注个人微信公众 ...

  2. Java8新特性 Stream流式思想(二)

    如何获取Stream流刚开始写博客,有一些不到位的地方,还请各位论坛大佬见谅,谢谢! package cn.com.zq.demo01.Stream.test01.Stream; import org ...

  3. Java8新特性Stream流应用示例

    Java8新特性介绍 过滤集合 List<String> newList = list.stream().filter(item -> item != null).collect(C ...

  4. java8 新特性Stream流的应用

    作为一个合格的程序员,如何让代码更简洁明了,提升编码速度尼. 今天跟着我一起来学习下java 8  stream 流的应用吧. 废话不多说,直入正题. 考虑以下业务场景,有四个人员信息,我们需要根据性 ...

  5. Java8新特性 Stream流式思想(一)

    遍历及过滤集合中的元素使用传统方式遍历及过滤集合中的元素package cn.com.zq.demo01.Stream.test01.Stream; import java.util.ArrayLis ...

  6. Java8新特性 Stream流式思想(三)

    Stream接口中的常用方法 forEach()方法package cn.com.cqucc.demo02.StreamMethods.Test02.StreamMethods; import jav ...

  7. Java8 新特性 —— Stream 流式编程

    本文部分摘自 On Java 8 流概述 集合优化了对象的存储,大多数情况下,我们将对象存储在集合是为了处理他们.使用流可以帮助我们处理对象,无需迭代集合中的元素,即可直接提取和操作元素,并添加了很多 ...

  8. java8 新特性 Stream流 分组 排序 过滤 多条件去重

    private static List<User> list = new ArrayList<User>(); public static void main(String[] ...

  9. Java8 新特性 Stream 无状态中间操作

    无状态中间操作 Java8 新特性 Stream 练习实例 中间无状态操作,可以在单个对单个的数据进行处理.比如:filter(过滤)一个元素的时候,也可以判断,比如map(映射)... 过滤 fil ...

随机推荐

  1. C#开发BIMFACE系列10 服务端API之获取文件下载链接

    系列目录     [已更新最新开发文章,点击查看详细] 通过BIMFACE控制台或者调用服务接口上传文件成功后,默认场景下需要下载该源文件,下载文件一般需要知道文件的下载链接即可.BIMACE平台提供 ...

  2. springboot之swagger快速启动

    springboot之swagger快速启动 简介 介绍 可能大家都有用过swagger,可以通过ui页面显示接口信息,快速和前端进行联调. 没有接触的小伙伴可以参考官网文章进行了解下demo页面. ...

  3. Codeforces 416D Population Size

    Population Size 题意: 一共n个数, 每个-1都可以变成一个正数, 现在要求最少数目的等差子序列,并且在这个子序列必须要连着截取一段,不能分开截取. 样例1: 8 6 4 2 1 4 ...

  4. java课堂测试样卷-----简易学籍管理系统

    程序设计思路:分别建立两个类:ScoreInformation类(用来定义学生的基本信息以及设置set和get函数)ScoreManagement类(用来定义实现学生考试成绩录入,考试成绩修改,绩点计 ...

  5. C语言实现二级指针表示字符串数组

    头文件: #include<stdlib.h> #include<stdio.h> #include<string.h> 函数原型: char ** createB ...

  6. [1]尝试用Unity3d制作一个王者荣耀(持续更新)->选择英雄-(上)

    如果已经看过本章节:目录传送门:这是目录鸭~ 1.场景搭建: 首先我们去AssetStore逛淘宝~~~ 我淘到的是这个资源,其他好看的场景(消耗不高的都行). 然后我们导入了这个资源后,把资源根文件 ...

  7. day02小结

    数据类型的转换 1,自动类型转换 byte,short,char-->int-->long-->float-->double (1)byte与byte,short与short, ...

  8. linux非root用户下安装软件,搭建生产环境

    之前的用实验室的服务器,因为某些原因,使用的用户没有root权限.linux的非root用户很多软件无法安装,非常的不方便.我的方法是使用brew来代替系统的包管理工具.brew是最先用在mac上的包 ...

  9. jquery ajax到servlet出现中文乱码(utf-8编码下)

    个人遇到的该问题有两大类: 第一类很普遍,就是jsp页面编码没有规定,servlet中接收参数没有转码,response没有使用setContentType()和setCharacterEncodin ...

  10. 自定义View入门-绘制基础(1)

    ### 前言 说道自定义View,我们一定会想到,自定义View的绘制流程 - 测量阶段(measure) - 布局阶段(layout) - 绘制阶段(draw) 我们看到的一些炫酷的view效果,都 ...