欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVA JDK8 List分组的实现和用法

程序员文章站 2024-03-01 18:02:16
概述 对list进行分组是日常开发中,经常遇到的,在jdk 8中对list按照某个属性分组的代码,超级简单。 package test; import com...

概述

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"
   ]
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接