您好,登錄后才能下訂單哦!
概述
對List
進(jìn)行分組是日常開發(fā)中,經(jīng)常遇到的,在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,"優(yōu)惠券1"); Coupon coupon2 = new Coupon(2,200,"優(yōu)惠券2"); Coupon coupon3 = new Coupon(3,300,"優(yōu)惠券3"); Coupon coupon4 = new Coupon(3,400,"優(yōu)惠券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
一樣的,歸為一組。打印結(jié)果如下:
{
1:[
{
"couponId":1,
"name":"優(yōu)惠券1",
"price":100
}
],
2:[
{
"couponId":2,
"name":"優(yōu)惠券2",
"price":200
}
],
3:[
{
"couponId":3,
"name":"優(yōu)惠券3",
"price":300
},
{
"couponId":3,
"name":"優(yōu)惠券4",
"price":400
}
]
}
如果分組后,分組內(nèi)并不想是對象,而是對象的屬性,也可以做到的。
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,"優(yōu)惠券1"); Coupon coupon2 = new Coupon(2,200,"優(yōu)惠券2"); Coupon coupon3 = new Coupon(3,300,"優(yōu)惠券3"); Coupon coupon4 = new Coupon(3,400,"優(yōu)惠券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)); } }
這樣分組內(nèi)就是name
屬性了。打印結(jié)果如下:
{
1:[
"優(yōu)惠券1"
],
2:[
"優(yōu)惠券2"
],
3:[
"優(yōu)惠券3",
"優(yōu)惠券4"
]
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對億速云的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。