java8 集合神操作
public class StreamUtils {
private static final List<Integer> listInteger = Lists.newArrayList(1, 2, 3, 4, 5, 6, 3, 5, 1, 4, 2, 8, 9);
private static final List<Integer> arrayList = Lists.newArrayList(1, 25, 6, 9, 22, 44);
public static void main(String[] args) {
///取%2的数
List<Integer> collect = listInteger.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
System.out.println(collect);
///去重
List<Integer> collect1 = listInteger.stream().distinct().collect(Collectors.toList());
System.out.println(collect1);
///跳过前面3个元素
List<Integer> collect2 = listInteger.stream().skip(3).collect(Collectors.toList());
System.out.println(collect2);
///取前面3个元素
List<Integer> collect3 = listInteger.stream().limit(3).collect(Collectors.toList());
System.out.println(collect3);
///打印dish getName集合
List<String> collect4 = list.stream().map(Dish::getName).collect(Collectors.toList());
System.out.println(collect4);
String[] helloWord = {"hellow", "word"};
///{h,e,l,l,o,w},{w,o,r,d}
Stream<String[]> stream = Arrays.stream(helloWord).map(s -> s.split(""));
///h,e,l,l,o,w,w,o,r,d || flatMap 扁平化操作接受stream
Stream<String> stringStream = stream.flatMap(Arrays::stream);
///去重
stringStream.distinct().forEach(System.out::println);
//allMatch 所有的元素的满足条件
System.out.println(arrayList.stream().allMatch(i -> i > 50));
///anyMatch 当元素数组中有一个元素满足就返回true
System.out.println(arrayList.stream().anyMatch(i -> i > 40));
///noneMatch 没有一个元素满足的情况下返回true
System.out.println(arrayList.stream().noneMatch(i -> i < 0));
///findAny随机获取一个元素
Optional<Integer> any = arrayList.stream().filter(i -> i > 2).findAny();
System.out.println(any.get());
///Options 中的orElse 如果返回结果是null使用orElse可以设置默认值,返回-1
Integer integer = arrayList.stream().filter(i -> i > 66).findAny().orElse(-1);
System.out.println(integer);
///isPresent元素是否存在,ifPresent 元素存在需要做什么事情
Optional<Integer> first = arrayList.stream().filter(i -> i > 10).findFirst();
System.out.println("optional元素是否存在:"+first.isPresent());
first.ifPresent(System.out::println);
//reduce 聚合函数 将数组中的元素累加 0设置默认值初始值
Integer sum = arrayList.stream().reduce(0, (x, y) -> x + y);
System.out.println(sum);
///打印数组中累加的值
arrayList.stream().reduce((x,y)->x+y).ifPresent(System.out::println);
///获取数组中的最大值
System.out.println(arrayList.stream().reduce(Integer::max).get());
///获取数组最小值
System.out.println(arrayList.stream().reduce(Integer::min).get());
///累加
arrayList.stream().reduce(Integer::sum).ifPresent(System.out::println);
73
///根据name分组
Map<String, List<UserInfo>> collect = listUser.stream().collect(Collectors.groupingBy(UserInfo::getName));
System.out.println(JSON.toJSONString(collect)); ///Collectors.averagingDouble 取出平均值
Optional.ofNullable(list.stream().collect(Collectors.averagingDouble(Dish::getOalories)))
.ifPresent(System.out::println); ///collectingAndThen 对结果进行处理
Optional.ofNullable(list.stream().collect(Collectors.collectingAndThen(Collectors.averagingDouble(Dish::getOalories),(a->"平均值:"+a))))
.ifPresent(System.out::println); List<Dish> dishList = list.stream().filter(d -> d.getType().equals(Dish.Type.OTHER)).collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)); // dishList.add(new Dish("salmon", false, 550, Dish.Type.FISH)); System.out.println(JSON.toJSONString(dishList)); ///打印集合个数
Optional.ofNullable(list.stream().collect(Collectors.counting())).ifPresent(System.out::println);
///{OTHER=4, MEAT=3, FISH=2} 分组之后统计分组的个数
Optional.ofNullable(list.stream().collect(Collectors.groupingBy(Dish::getType,Collectors.counting()))).ifPresent(System.out::println);
///分组之后 求出平均值 并且返回的TreeMap
Optional.ofNullable(list.stream().collect(Collectors.groupingBy(Dish::getType, TreeMap::new,Collectors.averagingDouble(Dish::getOalories)))).ifPresent(System.out::println);
///DoubleSummaryStatistics 统计集合的值 DoubleSummaryStatistics{count=9, sum=4200.000000, min=120.000000, average=466.666667, max=800.000000}
DoubleSummaryStatistics summaryStatistics = list.stream().collect(Collectors.summarizingDouble(Dish::getOalories));
System.out.println(summaryStatistics.toString()); ///concurrentMap 和 Map使用一样
ConcurrentMap<Dish.Type, List<Dish>> collect1 = list.stream().collect(Collectors.groupingByConcurrent(Dish::getType));
System.out.println(collect1);
///转换为skipListMap
ConcurrentSkipListMap<Dish.Type, Double> collect2 = list.stream().collect(Collectors.groupingByConcurrent(Dish::getType, ConcurrentSkipListMap::new, Collectors.averagingDouble(Dish::getOalories))); String collect3 = list.stream().collect(Collectors.mapping(Dish::getName, Collectors.joining(",", "[", "]"))); System.out.println(collect3);
}
}
java8 集合神操作的更多相关文章
- Java8 集合相关操作
// java8 集合快速转成string List<String> cities; String citiesCommaSeparated = String.join(",&q ...
- json和字符串/数组/集合的互相转换の神操作总结
一:前端字符串转JSON的4种方式 1,eval方式解析,恐怕这是最早的解析方式了. function strToJson(str){ var json = eval('(' + str + ')') ...
- 【java多线程】java8的流操作api和fork/join框架
原文:https://blog.csdn.net/u011001723/article/details/52794455/ 一.测试一个案例,说明java8的流操作是并行操作 1.代码 package ...
- Java8集合框架——基本知识点
前言 Java的基础集合框架的内容并不复杂,List.Map.Set 中大概10个常见的集合类,建议多看几遍源码(Java8),然后回过头再来看看这些各路博客总结的知识点,会有一种豁然开朗的感觉. 本 ...
- Java8集合框架——LinkedList源码分析
java.util.LinkedList 本文的主要目录结构: 一.LinkedList的特点及与ArrayList的比较 二.LinkedList的内部实现 三.LinkedList添加元素 四.L ...
- paip.数组以及集合的操作uapi java php python总结..
paip.数组以及集合的操作uapi 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...
- IT第二十一天 - Collections、ArrayList集合、LinkedList集合、Set集合、HashMap集合、集合的操作注意【修20130828】
NIIT第二十一天 上午 集合 1. 集合Collection存储数据的形式是单个存储的,而Map存储是按照键值对来存储的,键值对:即键+值同时存储的,类似align="center&quo ...
- java集合框架工具类Collections,集合的操作
1 import java.util.*; public class asList { public static void main(String args[]) { // int arr[] = ...
- java集合的操作(set,Iterator)
集合的操作 Iterator.Collection.Set和HashSet关系 Iterator<——Collection<——Set<——HashSet Iterator中的方法: ...
随机推荐
- Python中super()的用法
参考链接:https://www.cnblogs.com/shengulong/p/7892266.html super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是 ...
- 写vue项目时候 零星的笔记
1,挂载也可以用 .$mount() 2,子组件中通过this.$root拿到实例的数据.截图中是子组件中
- Upgrade Win10
Internal deployment of Windows 10 Enterprise is currently underway as a phased deployment. Watch you ...
- iOS-QQ临时对话、QQ群申请跳转
QQ 临时对话 NSString *qq = [NSString stringWithFormat:@"mqq://im/chat?chat_type=wpa&uin=%@& ...
- 6_文件IO
1. 基本文件读取 readline(),readlines(),write(),writelines() f.read(size),指定读取文件的字节数,需要注意的是 ...
- 桶排序和计数排序的理解实现和比较(Java)
比较和非比较的区别 常见的快速排序.归并排序.堆排序.冒泡排序等属于比较排序.在排序的最终结果里,元素之间的次序依赖于它们之间的比较.每个数都必须和其他数进行比较,才能确定自己的位置.比较排序的优势是 ...
- 解决 在 WINDOWS 下 同时安装 python2 python3 后 pip 错误
再之前同时安装 python 后 只需把环境变量PATH 里面改为 PATH=C:\Python36-32\Scripts\;C:\Python36-32\;C:\Python27\;C:\Pytho ...
- Django F()表达式
Django F()表达式 一个F()对象代表一个模型字段的值或注释列.使用它可以直接引用模型字段的值并执行数据库操作而不用把它们导入到python的内存中. 相反,Django使用F()对象生成一个 ...
- tensorflow进阶篇-4(损失函数3)
Softmax交叉熵损失函数(Softmax cross-entropy loss)是作用于非归一化的输出结果只针对单个目标分类的计算损失.通过softmax函数将输出结果转化成概率分布,然后计算真值 ...
- JNI 简单例子
原文:http://www.cnblogs.com/youxilua/archive/2011/09/16/2178554.html 1,先把c语言的编译环境搭建好,windows下这里使用mingw ...