评论

收藏

[Java] JAVA JDK8 List分组的实现和用法

编程语言 编程语言 发布于:2021-10-07 10:46 | 阅读数:451 | 评论:0

今天小编就为大家分享一篇关于JAVA JDK8 List分组的实现和用法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
概述
对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"
   ]
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对CodeAE代码之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/linsongbin1/article/details/83933703

关注下面的标签,发现更多相似文章