概述
对list
进行分组是日常开发中,经常遇到的,在jdk 8
中对list
按照某个属性分组的代码,超级简单。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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));
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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
}
]
}
如果分组后,分组内并不想是对象,而是对象的属性,也可以做到的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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