一、基本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. Linux之Shell编程(14)

    变量: 定义变量的规则: 1)变量名可以由字母.数字和下划线组成,但不能以数字开头 2)等号两侧不能有空格 3)变量名一般习惯大写 将命令的返回值赋值给变量: 1)使用``将命令括起来 2)使用$() ...

  2. 定时器Timer的运用

    1.Timer调度任务的方法

  3. P2762 太空飞行计划问题 最大权闭合子图

    link:https://www.luogu.org/problemnew/show/P2762 题意 承担实验赚钱,但是要花去对应仪器的费用,仪器可能共用.求最大的收益和对应的选择方案. 思路 这道 ...

  4. CF1007B Pave the Parallelepiped 容斥原理

    Pave the Parallelepiped time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. happen before 原则

    并发一直都是程序开发者绕不开的难题,在上一篇文章中我们知道了导致并发问题的源头是 : 多核 CPU 缓存导致程序的可见性问题.多线程间切换带来的原子性问题以及编译优化带来的顺序性问题. 原子性问题我们 ...

  6. 【Mac】快速复制文件路径

    一.使用Automator新建服务 二.配置 三.使用 四.创建快捷键 一.使用Automator新建服务 在应用程序文件夹里打开Automator,选择文件菜单,新建一个服务,如下  二.配置 在 ...

  7. 数据库常用SQL语句(一):常用的数据库、表操作及单表查询语句

    以MySql数据库为例进行说明 1.数据库操作语句 2.表的操作语句 3.表中的字段操作语句 4.MYSQL支持的完整性约束 数据库管理系统提供了一致机制来检查数据库表中的数据是否满足规定的条件,以保 ...

  8. springboot统一ajax返回数据格式,并且jquery ajax success函数修改

    服务端代码: package com.zhqn.sc.cfg; import org.springframework.core.MethodParameter; import org.springfr ...

  9. js关系运算符的用法和区别

    var num = 1;   var str = '1';   var test = 1;   test == num   //true 相同类型 相同值   test === num  //true ...

  10. 虚拟化(三) -vsphere套件的安装注意及使用

    https://www.cnblogs.com/zhrngM/p/9547958.html [转]虚拟化(三):vsphere套件的安装注意及使用 vsphere套件里面主要的组件有esxi.vcen ...