木头人
2018-08-08 10:08:04
java8并行流处理实例
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
import java.util.stream.Stream;
/**
* @Auther: jack
* @Date: 2018/7/24 11:05
* @Description: jdk8 并行流处理
*/
public class StreamParallel {
public static void main(String[] args) {
//rangeClosed包含末尾数据
long sum=LongStream.rangeClosed(1,10000000L).parallel().reduce(0,Long::sum);
System.out.println(sum);
//range不包含末尾节点
sum=LongStream.range(1,10000000L).parallel().reduce(0,Long::sum);
System.out.println(sum);
//求和reduce()
System.out.println(Stream.of(1,2,3,4).reduce((sum2,item)->{
return sum2+item;
}).get());
System.out.println(Stream.of(1,2,3,4).reduce(Integer::sum).get());
//求最大值reduce()
System.out.println(Stream.of(1,2,3,4).reduce(100,(a,b)->Math.max(a, b)));
System.out.println(Stream.of(1,2,3,4).reduce(Integer::max).get());
//去重distinct()
System.out.println(Arrays.asList("a", "b", "c", "d", "a", "b", "c")
.stream()
.distinct()
.collect(Collectors.toList())
);
//统计分组
System.out.println(Arrays.asList("apple", "apple", "banana",
"apple", "orange", "banana", "papaya")
.stream()
.collect(Collectors.groupingBy(Function.identity(),Collectors.counting()))
);
//统计排序
Map<String,Long> sortMap=new LinkedHashMap<>();
Arrays.asList("apple", "apple", "banana", "apple", "orange", "banana", "papaya")
.stream()
.collect(Collectors.groupingBy(Function.identity(),Collectors.counting()))
.entrySet()
.stream()
.sorted(Map.Entry.<String, Long>comparingByValue().reversed())
.forEachOrdered(e->sortMap.put(e.getKey(),e.getValue()));
System.out.println(sortMap);
}
}
由最代码官方编辑于Aug 8, 2018 2:50:25 PM
猜你喜欢
- java并行执行任务的框架Fork/Join的使用实例
- SpringMVC的三种统一异常处理实例代码分享
- spring的批量处理框架spring-batch简单实例,一看就懂
- Python处理彩色图片为黑白铅笔素描图
- Spring Boot学习(六)之Web应用的统一异常处理博客 源码分享
- maven工程采用xxl-excel框架实现excel的输入输出excel文件,支持xlsx、xls格式
- Spring MVC自定义异常堆栈处理代码
- java常用流处理工具StreamTool 常见的InputStream流转字符串, 转字节数组等等
- java实现图片的裁剪功能
- java图片裁剪处理工具类代码
- windows下定时打开浏览器
- java修改word文档(并非是poi、jacob、java2word)
请下载代码后再发表评论
相关代码
最近下载
最近浏览
浪里格朗 LV4
2023年1月31日
jxdgogogo
2020年11月9日
暂无贡献等级
sularman LV2
2020年10月23日
linjh123 LV1
2020年7月2日
zdm_ziming LV1
2020年6月19日
a1677596408 LV23
2020年6月16日
wdeshijie LV11
2020年6月12日
taocui LV2
2020年5月19日
dg_wangjianlin LV2
2020年3月4日
wkc LV21
2020年2月3日






