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 ...
随机推荐
- sql基本
SELECT: select * from table select 列名 from table select DISTINCT 列名 from table INSERT: insert into t ...
- ASCII编码查看
实例说明 ASCII是American Standard Code Information Interchange的缩写,是基于拉丁字母的一套电脑编码系统,主要用于显示英文字符,是目前世界上最通用的单 ...
- ASP.NET中Dataset的table数据合并、数据截取、数据排序
1.两个相同字段表的合并: public static DataSet CombineTables(DataSet _ds, DataTable _dt1, DataTable _dt2) { Dat ...
- treesoft,couchDB,
下载 docker 镜像:docker pull docker.io/lu566/treesoft:1.0启动容器:docker run -d -p 127.0.0.1:18080:8080 dock ...
- Elasticsearch学习笔记(五)索引元数据和集群元数据
一.索引元数据 执行:GET /ecommerce/product/1 返回结果: { "_index": "ecommerce", "_ty ...
- VUE-003-前端表格数据展示时,设置单元格(el-table-column)保留空格和换行
在使用 el-table 展示数据时,单元格中的数据有可能存在空格和换行符,若不进行设置,浏览器默认会取消空格和换行符,如下所示: 解决方法: 将单元格的样式 “white-space” 属性设置为“ ...
- Windows服务器外网无法访问web的解决方法
windows环境下使用集成 IIS服务器时一般不会发生外网无法访问的问题,而使用apache.kangle.lighttpd.niginx.tomcat等时:服务器上可通过配置的域名访问网站,pin ...
- 2019春第十周作业Compile Summarize
这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 在这里 我在这个课程的目标是 能够对C语言的编写更加得心应手 这个作业在那个具体方面帮助我实现目标 结构体更进一步 参考文献与网址 C语言 ...
- 跨平台技术实践案例: 用 reactxp 重写墨刀的移动端
Authors: Gao Cong, Perry Poon Illustrators: Shena Bian April 20, 2019 重新编写,又一次,我们又一次重新编写了移动端应用和移动端 ...
- linux云计算面试知识汇总,文件共享--vsftpd秘籍
随着天气的升温快要步入夏天,紧随着面试季来喽,想知道Linux运维面试时都会被问到哪些问题吗?特整理出这些资料给大家参考学习,据说知道全部答案的人,月薪能过万,还不快快码下,以备后用! 今天小编将继续 ...