Java8新特性——stream流
一、基本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流的更多相关文章
- 这可能是史上最好的 Java8 新特性 Stream 流教程
本文翻译自 https://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/ 作者: @Winterbe 欢迎关注个人微信公众 ...
- Java8新特性 Stream流式思想(二)
如何获取Stream流刚开始写博客,有一些不到位的地方,还请各位论坛大佬见谅,谢谢! package cn.com.zq.demo01.Stream.test01.Stream; import org ...
- Java8新特性Stream流应用示例
Java8新特性介绍 过滤集合 List<String> newList = list.stream().filter(item -> item != null).collect(C ...
- java8 新特性Stream流的应用
作为一个合格的程序员,如何让代码更简洁明了,提升编码速度尼. 今天跟着我一起来学习下java 8 stream 流的应用吧. 废话不多说,直入正题. 考虑以下业务场景,有四个人员信息,我们需要根据性 ...
- Java8新特性 Stream流式思想(一)
遍历及过滤集合中的元素使用传统方式遍历及过滤集合中的元素package cn.com.zq.demo01.Stream.test01.Stream; import java.util.ArrayLis ...
- Java8新特性 Stream流式思想(三)
Stream接口中的常用方法 forEach()方法package cn.com.cqucc.demo02.StreamMethods.Test02.StreamMethods; import jav ...
- Java8 新特性 —— Stream 流式编程
本文部分摘自 On Java 8 流概述 集合优化了对象的存储,大多数情况下,我们将对象存储在集合是为了处理他们.使用流可以帮助我们处理对象,无需迭代集合中的元素,即可直接提取和操作元素,并添加了很多 ...
- java8 新特性 Stream流 分组 排序 过滤 多条件去重
private static List<User> list = new ArrayList<User>(); public static void main(String[] ...
- Java8 新特性 Stream 无状态中间操作
无状态中间操作 Java8 新特性 Stream 练习实例 中间无状态操作,可以在单个对单个的数据进行处理.比如:filter(过滤)一个元素的时候,也可以判断,比如map(映射)... 过滤 fil ...
随机推荐
- C语言打印图形
//输出图形 * * * * * * * * * * * * * * * * * * * * * * * * * //每行5个* void test1(){ // 外层负责行,外层执行一次,内层 ...
- python控制窗口缩放
import win32gui import win32con import time # 使用之前先打开一个记事本 notepad = win32gui.FindWindow("Notep ...
- 2019 Multi-University Training Contest 6
A.Salty Fish upsolved 题意 偷苹果,每个节点上有\(a[i]\)个苹果,在某些位置有摄像机,看管子树里距离不超过\(k[i]\)的节点,损坏摄像机有\(c[i]\)代价,求最大收 ...
- light 1205 - Palindromic Numbers(数位dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1205 题解:这题作为一个数位dp,是需要咚咚脑子想想的.这个数位dp方程可能不 ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- MPA JS CSS预处理方案
1.WebPack 添加配置文件webpack.config.js,直接在当前目录运行 webpack. var basepath = '/root/webapps/happ'; var glob = ...
- R语言基础入门
请先安装好R和RStudio 如果不干别的,控制台就是一个内置计算器 2 * 3 #=> 6 sqrt(36) #=> 6, square root log10(100) #=> 2 ...
- 行数据库VS列数据库
一.介绍 目前大数据存储有两种方案可供选择:行存储和列存储.业界对两种存储方案有很多争持,集中焦点是:谁能够更有效地处理海量数据,且兼顾安全.可靠.完整性.从目前发展情况看,关系数据库已经不适应这种巨 ...
- js 正则表达式:价格的校验
/*验证单价:包括两位小数*/var priceReg = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;var price=$("#price& ...
- Day 19 磁盘管理
1.磁盘的基本概念 1.什么是磁盘 磁盘(disk)是指利用磁记录技术存储数据的存储器. 磁盘是计算机主要的存储介质,可以存储大量的二进制数据,并且断电后也能保持数据不丢失. *绝大多数人对硬盘都不陌 ...