【java stream 使用案例】根据某个字段进行分组

java 复制代码
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class demo2 {

    public static void main(String[] args) {
        List<OrderEntity> list = getListData();
        // 根据类型进行分组
        Map<String, List<OrderEntity>> collect = list.stream().collect(Collectors.groupingBy(OrderEntity::getType));
        // 取得类型为02的数据
        List<OrderEntity> list02 = collect.get("02");
        System.out.println(list02.toString());
    }

    public static List<OrderEntity> getListData() {
        List<OrderEntity> list = new ArrayList<>();
        OrderEntity o1 = new OrderEntity("1", "商品1", "189.99", "2025-01-01", "18:20:18", "01");
        list.add(o1);
        OrderEntity o2 = new OrderEntity("2", "商品2", "289.98", "2025-02-01", "14:28:40", "02");
        list.add(o2);
        OrderEntity o3 = new OrderEntity("3", "商品3", "589.99", "2024-08-01", "11:20:24", "02");
        list.add(o3);
        OrderEntity o4 = new OrderEntity("4", "商品4", "389.98", "2024-11-21", "18:28:55", "01");
        list.add(o4);
        OrderEntity o5 = new OrderEntity("5", "商品5", "859.99", "2024-09-11", "05:20:42", "01");
        list.add(o5);
        OrderEntity o6 = new OrderEntity("6", "商品6", "889.98", "2024-12-24", "16:18:52", "01");
        list.add(o6);
        OrderEntity o7 = new OrderEntity("7", "商品7", "198.99", "2025-02-09", "19:15:35", "02");
        list.add(o7);
        OrderEntity o8 = new OrderEntity("8", "商品8", "129.98", "2024-01-18", "12:25:56", "01");
        list.add(o8);
        return list;
    }

}
java 复制代码
import java.math.BigDecimal;

public class OrderEntity {
    /**
     * id
     */
    private String id;
    /**
     * 名称
     */
    private String name;
    /**
     * 价格
     */
    private BigDecimal price;
    /**
     * 购买日期
     */
    private String buyDate;
    /**
     * 购买时间
     */
    private String buyTime;
    /**
     * 商品类型
     */
    private String type;

    public OrderEntity() {}

    public OrderEntity(String id, String name, String price, String buyDate, String buyTime, String type) {
        this.id = id;
        this.name = name;
        this.price = new BigDecimal(price);
        this.buyDate = buyDate;
        this.buyTime = buyTime;
        this.type = type;
    }

    public String getId() {
        return this.id;
    }

    public void setId(final String id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public BigDecimal getPrice() {
        return this.price;
    }

    public void setPrice(final BigDecimal price) {
        this.price = price;
    }

    public String getBuyDate() {
        return this.buyDate;
    }

    public void setBuyDate(final String buyDate) {
        this.buyDate = buyDate;
    }

    public String getBuyTime() {
        return this.buyTime;
    }

    public void setBuyTime(final String buyTime) {
        this.buyTime = buyTime;
    }

    public String getType() {
        return this.type;
    }

    public void setType(final String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "OrderEntity{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", buyDate='" + buyDate + '\'' +
                ", buyTime='" + buyTime + '\'' +
                ", type='" + type + '\'' +
                '}';
    }
}
相关推荐
fly-phantomWing40 分钟前
Maven的安装与配置的详细步骤
java·后端·maven·intellij-idea
2401_841495644 小时前
【数据结构】红黑树的基本操作
java·数据结构·c++·python·算法·红黑树·二叉搜索树
学编程的小鬼4 小时前
SpringBoot 自动装配原理剖析
java·spring boot·后端
@@神农5 小时前
maven的概述以及在mac安装配置
java·macos·maven
杜子不疼.5 小时前
【C++】玩转模板:进阶之路
java·开发语言·c++
夜晚中的人海5 小时前
【C++】异常介绍
android·java·c++
Le1Yu6 小时前
2025-9-28学习笔记
java·笔记·学习
C++chaofan6 小时前
项目中为AI添加对话记忆
java·数据结构·人工智能·redis·缓存·个人开发·caffeine
老华带你飞6 小时前
机电公司管理小程序|基于微信小程序的机电公司管理小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·微信小程序·小程序·机电公司管理小程序
拾忆,想起7 小时前
AMQP协议深度解析:消息队列背后的通信魔法
java·开发语言·spring boot·后端·spring cloud