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 集合神操作的更多相关文章

  1. Java8 集合相关操作

    // java8 集合快速转成string List<String> cities; String citiesCommaSeparated = String.join(",&q ...

  2. json和字符串/数组/集合的互相转换の神操作总结

    一:前端字符串转JSON的4种方式 1,eval方式解析,恐怕这是最早的解析方式了. function strToJson(str){ var json = eval('(' + str + ')') ...

  3. 【java多线程】java8的流操作api和fork/join框架

    原文:https://blog.csdn.net/u011001723/article/details/52794455/ 一.测试一个案例,说明java8的流操作是并行操作 1.代码 package ...

  4. Java8集合框架——基本知识点

    前言 Java的基础集合框架的内容并不复杂,List.Map.Set 中大概10个常见的集合类,建议多看几遍源码(Java8),然后回过头再来看看这些各路博客总结的知识点,会有一种豁然开朗的感觉. 本 ...

  5. Java8集合框架——LinkedList源码分析

    java.util.LinkedList 本文的主要目录结构: 一.LinkedList的特点及与ArrayList的比较 二.LinkedList的内部实现 三.LinkedList添加元素 四.L ...

  6. paip.数组以及集合的操作uapi java php python总结..

    paip.数组以及集合的操作uapi 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...

  7. IT第二十一天 - Collections、ArrayList集合、LinkedList集合、Set集合、HashMap集合、集合的操作注意【修20130828】

    NIIT第二十一天 上午 集合 1. 集合Collection存储数据的形式是单个存储的,而Map存储是按照键值对来存储的,键值对:即键+值同时存储的,类似align="center&quo ...

  8. java集合框架工具类Collections,集合的操作

    1 import java.util.*; public class asList { public static void main(String args[]) { // int arr[] = ...

  9. java集合的操作(set,Iterator)

    集合的操作 Iterator.Collection.Set和HashSet关系 Iterator<——Collection<——Set<——HashSet Iterator中的方法: ...

随机推荐

  1. 迁移桌面程序到MS Store(7)——APPX + Service

    本篇我们以一个Sample工程,来说明如何把一个常见结构的desktop application,转制成APPX并在MS Store提供下载. 之前的篇章中,我们已经介绍了一些内容,包括如何通过Vis ...

  2. 浏览器环境下JavaScript脚本加载与执行探析之defer与async特性

    defer和async特性相信是很多JavaScript开发者"熟悉而又不熟悉"的两个特性,从字面上来看,二者的功能很好理解,分别是"延迟脚本"和"异 ...

  3. UICollectionView设置首个cell默认选中(二)

    上篇对于UICollectionView默认选中cell采取的是每个cell分别对应一个标识,也就代表着废除了UICollectionView的重用机制.对于较少的数据情况是可以的,但是对于数据比较大 ...

  4. 跟踪spring MVC的请求

    当我们点击一个超链接时,spring MVC在后台都做了些什么呢,今天就来看看后台都干了啥 首先需要在web.xml里配置一下:

  5. vue+iview实现一行平均五列布局

    iview 的栅格布局是以 html代码部分: <Row :gutter="20"> <Col style="float: left;width: 20 ...

  6. [视频]youku与56客户端DLL却持

    [视频]youku与56客户端DLL却持 https://pan.baidu.com/s/1c1ArnOk

  7. POJ 2552

    #include<iostream> #include<stdio.h> using namespace std; ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ...

  8. Spring WebSocket踩坑指南

    Spring WebSocket踩坑指南 本次公司项目中需要在后台与安卓App间建立一个长连接,这里采用了Spring的WebSocket,协议为Stomp. 关于Stomp协议这里就不多介绍了,网上 ...

  9. 剑指offer四之重建二叉树

    一.题目: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7 ...

  10. C# 多线程学习系列三之CLR线程池系列之ThreadPool

    一.CLR线程池 1.进程和CLR的关系一个进程可以只包含一个CLR,也可以包含多个CLR2.CLR和AppDomain的关系一个CLR可以包含多个AppDomain3.CLR和线程池的关系一个CLR ...