JAVA JDK8 List分组的实现和用法
作者:Sam哥哥 时间:2023-11-26 09:56:11
概述
对List
进行分组是日常开发中,经常遇到的,在JDK 8
中对List
按照某个属性分组的代码,超级简单。
package test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ListGroupTest {
public static void main(String[] args) {
List<Coupon> couponList = new ArrayList<>();
Coupon coupon1 = new Coupon(1,100,"优惠券1");
Coupon coupon2 = new Coupon(2,200,"优惠券2");
Coupon coupon3 = new Coupon(3,300,"优惠券3");
Coupon coupon4 = new Coupon(3,400,"优惠券4");
couponList.add(coupon1);
couponList.add(coupon2);
couponList.add(coupon3);
couponList.add(coupon4);
Map<Integer, List<Coupon>> resultList = couponList.stream().collect(Collectors.groupingBy(Coupon::getCouponId));
System.out.println(JSON.toJSONString(resultList, SerializerFeature.PrettyFormat));
}
}
package test;
public class Coupon {
private Integer couponId;
private Integer price;
private String name;
public Coupon(Integer couponId, Integer price, String name) {
this.couponId = couponId;
this.price = price;
this.name = name;
}
public Integer getCouponId() {
return couponId;
}
public void setCouponId(Integer couponId) {
this.couponId = couponId;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
上面的例子是对List
按照couponId
分组,couponId
一样的,归为一组。打印结果如下:
{
1:[
{
"couponId":1,
"name":"优惠券1",
"price":100
}
],
2:[
{
"couponId":2,
"name":"优惠券2",
"price":200
}
],
3:[
{
"couponId":3,
"name":"优惠券3",
"price":300
},
{
"couponId":3,
"name":"优惠券4",
"price":400
}
]
}
如果分组后,分组内并不想是对象,而是对象的属性,也可以做到的。
package test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ListGroupTest2 {
public static void main(String[] args) {
List<Coupon> couponList = new ArrayList<>();
Coupon coupon1 = new Coupon(1,100,"优惠券1");
Coupon coupon2 = new Coupon(2,200,"优惠券2");
Coupon coupon3 = new Coupon(3,300,"优惠券3");
Coupon coupon4 = new Coupon(3,400,"优惠券4");
couponList.add(coupon1);
couponList.add(coupon2);
couponList.add(coupon3);
couponList.add(coupon4);
Map<Integer, List<String>> resultList = couponList.stream().collect(Collectors.groupingBy(Coupon::getCouponId,Collectors.mapping(Coupon::getName,Collectors.toList())));
System.out.println(JSON.toJSONString(resultList, SerializerFeature.PrettyFormat));
}
}
这样分组内就是name
属性了。打印结果如下:
{
1:[
"优惠券1"
],
2:[
"优惠券2"
],
3:[
"优惠券3",
"优惠券4"
]
}
来源:https://blog.csdn.net/linsongbin1/article/details/83933703
标签:java,jdk,list
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java获取任意http网页源代码的方法
2022-07-06 01:45:37
详解JDK自带javap命令反编译class文件和Jad反编译class文件(推荐使用jad)
2021-12-24 00:29:29
![](https://img.aspxhome.com/file/2023/3/97803_0s.png)
C#单例模式与多线程用法介绍
2023-03-25 09:36:07
解决IDEA maven 项目修改代码不生效,mvn clean、install后才生效
2022-06-19 12:17:06
![](https://img.aspxhome.com/file/2023/2/97812_0s.jpg)
详解Java中使用泛型实现快速排序算法的方法
2022-04-28 09:47:00
C#操作windows系统进程的方法
2023-06-09 05:11:45
eclipse导入appcompat项目报错解决办法
2021-11-10 14:25:57
![](https://img.aspxhome.com/file/2023/6/116126_0s.png)
Android 定时器实现图片的变换
2021-05-28 00:33:13
WPF如何自定义TabControl控件样式示例详解
2021-09-22 07:27:34
![](https://img.aspxhome.com/file/2023/7/108827_0s.gif)
C和指针小结(推荐)
2022-11-08 09:17:08
![](https://img.aspxhome.com/file/2023/7/127527_0s.png)
Android性能优化之plt hook与native线程监控详解
2021-09-10 22:06:01
![](https://img.aspxhome.com/file/2023/9/138939_0s.jpg)
Android中activity处理返回结果的实现方式
2022-10-21 12:36:36
![](https://img.aspxhome.com/file/2023/6/138786_0s.gif)
Spring中的bean概念介绍
2023-01-18 07:46:49
![](https://img.aspxhome.com/file/2023/3/71863_0s.png)
Android AlertDialog(对话框)实例详解
2023-07-19 11:37:55
![](https://img.aspxhome.com/file/2023/6/126776_0s.jpg)
C#的静态工厂方法与构造函数相比有哪些优缺点
2022-08-29 21:11:45
springBoot加入thymeleaf模板的方式
2023-11-25 14:31:23
![](https://img.aspxhome.com/file/2023/6/60056_0s.png)
Ribbon单独使用,配置自动重试,实现负载均衡和高可用方式
2023-05-12 00:49:15
![](https://img.aspxhome.com/file/2023/6/116106_0s.jpg)
一篇文章弄懂JVM类加载机制过程以及原理
2022-01-19 10:32:57
![](https://img.aspxhome.com/file/2023/3/88143_0s.png)
java编程创建型设计模式单例模式的七种示例
2023-06-21 08:47:39
![](https://img.aspxhome.com/file/2023/7/77387_0s.png)
C#多态详解
2023-06-25 13:42:20
![](https://img.aspxhome.com/file/2023/1/78191_0s.png)