Java8分组(groupingBy)
1、分组,计数,排序
public class Java8Example1 {
public static void main(String[] args) {
List<String> items = Arrays.asList(
"apple", "apple",
"orange", "orange", "orange",
"blueberry",
"peach", "peach", "peach", "peach"
);
// 分组,计数
Map<String, Long> result = items.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(result);
Map<String, Long> finalMap = new LinkedHashMap<>();
// 排序
result.entrySet().stream()
.sorted(Map.Entry.<String, Long>comparingByValue().reversed())
.forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue()));
System.out.println(finalMap);
}
}
2、以下例子使用的类
public class Item {
private String name;
private int qty;
private BigDecimal price;
public Item() {
}
public Item(String name, int qty, BigDecimal price) {
this.name = name;
this.qty = qty;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
@Override
public String toString() {
return "Item{" +
"name='" + name + '\'' +
", qty=" + qty +
", price=" + price +
'}';
}
}
3、分组,计数,计算数量
public class Java8Example2 {
public static void main(String[] args) {
List<Item> items = Arrays.asList(
new Item("apple", 10, new BigDecimal(23.5)),
new Item("apple", 20, new BigDecimal(32.5)),
new Item("orange", 30, new BigDecimal(13.9)),
new Item("orange", 20, new BigDecimal(33.5)),
new Item("orange", 10, new BigDecimal(63.5)),
new Item("orange", 50, new BigDecimal(41.5)),
new Item("peach", 20, new BigDecimal(26.5)),
new Item("peach", 30, new BigDecimal(42.5)),
new Item("peach", 40, new BigDecimal(24.5)),
new Item("peach", 10, new BigDecimal(12.5))
);
// 分组,计数
Map<String, Long> counting = items.stream()
.collect(Collectors.groupingBy(Item::getName, Collectors.counting()));
System.out.println(counting);
// 分组,计数,数量
Map<String, Integer> sum = items.stream()
.collect(Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getQty)));
System.out.println(sum);
}
}
4、通过价格分组
public class Java8Example3 {
public static void main(String[] args) {
List<Item> items = Arrays.asList(
new Item("apple", 10, new BigDecimal(23.5)),
new Item("apple", 20, new BigDecimal(32.5)),
new Item("orange", 30, new BigDecimal(13.9)),
new Item("orange", 20, new BigDecimal(32.5)),
new Item("orange", 10, new BigDecimal(63.5)),
new Item("orange", 50, new BigDecimal(41.5)),
new Item("peach", 20, new BigDecimal(26.5)),
new Item("peach", 30, new BigDecimal(32.5)),
new Item("peach", 40, new BigDecimal(24.5)),
new Item("peach", 10, new BigDecimal(12.5))
);
// 分组
Map<BigDecimal, List<Item>> groupByPriceMap = items.stream()
.collect(Collectors.groupingBy(Item::getPrice));
System.out.println(groupByPriceMap);
// 分组 转化List->Set
Map<BigDecimal, Set<String>> result = items.stream()
.collect(Collectors.groupingBy(Item::getPrice, Collectors.mapping(Item::getName, Collectors.toSet())));
System.out.println(result);
}
}
Java8分组(groupingBy)的更多相关文章
- java8 先groupingBy 后map
Map<Integer,List<String>> mapBanJI_UserNameList=list.stream().collect(Collectors.groupin ...
- Java8 stream 中利用 groupingBy 进行多字段分组求和
Java8的groupingBy实现集合的分组,类似Mysql的group by分组功能,注意得到的是一个map 对集合按照单个属性分组 case1: List<String> items ...
- 乐字节-Java8新特性-接口默认方法之Stream流(下)
接上一篇:<Java8新特性之stream>,下面继续接着讲Stream 5.流的中间操作 常见的流的中间操作,归为以下三大类:筛选和切片流操作.元素映射操作.元素排序操作: 操作 描述 ...
- 乐字节-Java8核心特性实战之Stream(流)
说起流,我们会想起手机 ,电脑组装流水线,物流仓库商品包装流水线等等.如果把手机 ,电脑,包裹看做最终结果的话,那么加工商品前的各种零部件就可以看做数据源,而中间一系列的加工作业操作,就可以看做流的处 ...
- 简洁又快速地处理集合——Java8 Stream(下)
上一篇文章我讲解 Stream 流的基本原理,以及它与集合的区别关系,讲了那么多抽象的,本篇文章我们开始实战,讲解流的各个方法以及各种操作 没有看过上篇文章的可以先点击进去学习一下 简洁又快速地处理集 ...
- Java8初体验(二)Stream语法详解---符合人的思维模式,数据源--》stream-->干什么事(具体怎么做,就交给Stream)--》聚合
Function.identity()是什么? // 将Stream转换成容器或Map Stream<String> stream = Stream.of("I", & ...
- java8(2)--- Stream API
1.简述 Java8中有两大最为重要的改变.第一个是 Lambda 表达式:另外一 个就是 Stream API. Stream 是处理集合的抽象概念,它可以指定你希望对集合进行的操作,可以执行非常复 ...
- Java8 新特性之集合操作Stream
Java8 新特性之集合操作Stream Stream简介 Java 8引入了全新的Stream API.这里的Stream和I/O流不同,它更像具有Iterable的集合类,但行为和集合类又有所不同 ...
- JAVA8学习——从使用角度深入Stream流(学习过程)
Stream 流 初识Stream流 简单认识一下Stream:Stream类中的官方介绍: /** * A sequence of elements supporting sequential an ...
随机推荐
- Jmeter遇到打不开的问题
1.JDK的版本,一定版本的jmeter需要特定版本以上的JDK支持,比如此次运行的apache-jmeter-2.12,就需要JDK1.6以上的版本支持.我原来装的是JDK1.5,配置好JMETER ...
- p1257 平面上最接近点对---(分治法)
首先就是一维最接近点的情况... #include<iostream> #include<cstdio> #include<cstring> #include< ...
- Java根据链接生成二维码
Java根据链接生成二维码 相关 jar 包: core-3.1.0.jar 源码及 jar 包下载:http://files.cnblogs.com/files/liaolongjun/qrcode ...
- tomcat之过滤器
过滤器是一种特殊的servlet,也需要在配置文件中进行配置,通过它可以将指定的请求拦截下来,之后对请求处理,处理完之后,将拦截请求放行.实现过滤器也需要实现一个接口叫javax. servlet.F ...
- 5. Scala函数式编程的基础
5.1 函数式编程内容及介绍顺序说明 5.1.1 函数式编程内容 -函数式编程基础 函数定义/声明 函数运行机制 递归(难点,[最短路径,邮差问题,迷宫问题,回溯]) 过程 惰性函数和异常 -函数式编 ...
- ASCII Unicode UTF-8 之间的关系
转载请标明:https://i.cnblogs.com/EditPosts.aspx?opt=1 1. ASCII ASCII 只有127个字符,表示英文字母的大小写.数字和一些符号,但由于其他语言用 ...
- static与volatile的用法
static 1.概述 static 声明的变量在C语言中有两方面的特征: 1).变量会被放在程序的全局存储区中,这样可以在下一次调用的时候还可以保持原来的赋值.这一点是它与堆栈变量和堆变量的区别 ...
- centos7 + php7
安装php7 https://www.zntec.cn/archives/centos7-lamp.html composer安装tp curl -sS https://getcomposer.org ...
- 百度地图API实时画出动态运行轨迹(一条行驶轨迹),车头实时指向行驶方向,设置角度偏移
参考网址:https://blog.csdn.net/skywqnan/article/details/79036262 改变车的方向:http://www.cnblogs.com/peixuanzh ...
- 使用Gadget 做usb鼠标键盘设备
使用Gadget 做usb鼠标键盘设备 感谢TI社区提供的好帮助啊!http://e2e.ti.com/support/arm/sitara_arm/f/791/p/571771/2103409?pi ...