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中的方法: ...
随机推荐
- Button去除边框方法
<Button Content="Button" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey} ...
- 【react】当react框架遇上百度地图
百度地图官方文档的使用指导是这样说的:在页面中引入<script type="text/javascript" src="http://api.map.baid ...
- kls与flag(map)
题目传送门 这道题还挺搞笑的,\(map\)常数贼大还是把它水过了. 可以发现这道题求的是 \((j>i)j-i=h_i+h_j,j-i=|h_i-h_j|\)的对数. 那么显然,因为高度大于\ ...
- Django(ORM查询联系题)
day70 练习题:http://www.cnblogs.com/liwenzhou/articles/8337352.html import os import sys if __name__ == ...
- 一步步Cobol 400上手自学入门教程04 - 过程部
过程部的作用:编写程序要执行的语句,是程序的核心. 结构: 基本语句 INITIALIZE 设置数据项的初始值 ACCEPT 接收从键盘或指定设备上获得输入数据. 例子: 当批处理文件读到调用ACCP ...
- TortoiseSVN切换更改登录账号密码
TortoiseSVN切换更改登录账号密码 方法: 在TortoiseSVN的设置对话框中,选择“已保存数据”,在“认证数据”那一行点击“清除”按钮,清楚保存的认证数据,再检出的时候就会重新跳出用户名 ...
- Oracle VM VirtualBox启动后莫名奇妙的报错
VirtualBox软件无法启动: 参考解决:http://blog.csdn.net/a_ssimi/article/details/52002939 修改兼容性:http://blog.csdn. ...
- 通过修改CR0寄存器绕过SSDT驱动保护
为了安全起见,Windows XP及其以后的系统将一些重要的内存页设置为只读属性,这样就算有权力访问该表也不能随意对其修改,例如SSDT.IDT等.但这种方法很容易被绕过,我们只要将这些部分修改为可写 ...
- java8之lambda表达式(2)-方法引用
方法引用使用的地方也是在函数式接口,使用方法引用可以使代码更加简单和便捷 在如下代码中 根据List中字符串长度排序的代码可以写成如下: public static void test1_() { L ...
- 《松本行弘的程序世界》读书笔记(上)——面向对象、程序块、设计模式、ajax
1. 前言 半个月之前买了这本书,还是经园子里的一位网友推荐的.到现在看了一半多,基础的都看完了,剩下的几章可做高级部分来看.这本书看到现在,可以说感触很深,必须做一次读书笔记! 关于这本书,不了解的 ...