一、基本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. 谈谈我对SOFA模块化的理解

    今天我们谈谈SOFA模块化,首先看一段SOFA的介绍: SOFABoot是蚂蚁金服开源的基于Spring Boot的研发框架,它在Spring Boot的基础上,提供了诸如 Readiness Che ...

  2. .NET CORE下最快比较两个文件内容是否相同的方法 - 续

    .NET CORE下最快比较两个文件内容是否相同的方法 - 续 在上一篇博文中, 我使用了几种方法试图找到哪个是.NET CORE下最快比较两个文件的方法.文章发布后,引起了很多博友的讨论, 在此我对 ...

  3. 用故事解析setTimeout和setInterval(内含js单线程和任务队列)

    区别: setTimeout(fn,t): 延迟调用,超过了时间就调用回调函数,返回一个id,使用clearTimeout(id)取消执行. 注意:取消了里面的回调函数就不执行了哦,而不是取消的时候就 ...

  4. Unity进阶:PlayMaker

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  5. MARTIN FOWLER谈敏捷开发

    转自:http://www.scrumcn.com/agile/scrum-knowledge-library/agile-development.html#tab-id-9 每个人都在谈论敏捷开发( ...

  6. cogs 313. [POI2001] 和平委员会(2-SAT

    http://cogs.pro:8080/cogs/problem/problem.php?pid=pyzQimjkj 题意:有n个集合,每个集合有俩元素,要从n个中各选一个放一堆,但是有的俩不能同时 ...

  7. codeforces 626 G. Raffles(线段树+思维+贪心)

    题目链接:http://codeforces.com/contest/626/problem/G 题解:这题很明显买彩票肯定要买贡献最大的也就是说买p[i]*(num[i]+1)/(num[i]+a[ ...

  8. 牛客小白月赛6 B 范围 数学

    链接:https://www.nowcoder.com/acm/contest/135/B来源:牛客网 题目描述 已知与均为实数,且满足: 给定A,B,求x的取值范围? 由于Apojacsleam的计 ...

  9. KDTree 板子

    从杨哥哪里偷的板子, 存一下. #include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.t ...

  10. Codeforces Round #481 (Div. 3) B. File Name

    题目地址:http://codeforces.com/contest/978/problem/B 题解:一串文件名里不能出现连续的xxx,询问进行几次操作后,文件名才不会出现xxx. 方法:只要遍历一 ...