java 一层list 转两层分组list(分组key为list对象内的某个key)

本文介绍了一个实用的Java工具类,用于将List集合按指定字段进行分组,并提供了两种分组结果展示方式:一种是Map结构,另一种是List结构。通过示例展示了如何使用该工具类对包含金额和创建月份的账户列表进行分组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下:

import com.alibaba.fastjson.JSONObject;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * list分组工具类
 *
 * T 实体需要实现toString方法,toString结果要为json字符串
 *
 * @date 20181204
 * @author heatgemini
 */
public class DataGroupUtil {

    /**
     *
     * @param list 原一层list
     * @param groupKey 分组KEY
     * @param <T>
     * @return map
     */
    public static <T> Map<String, List<T>> list2Map(List<T> list, String groupKey) {

        Map<String, List<T>> resGroupMap = new LinkedHashMap<>();
        for (T map : list) {
            JSONObject json = JSONObject.parseObject(map.toString());
            if(!json.containsKey(groupKey)){
                continue;
            }
            String key = (String) json.get(groupKey);
            if (!resGroupMap.containsKey(key)) {
                List<T> listMap = new ArrayList<>();
                listMap.add(map);
                resGroupMap.put(key, listMap);
            } else {
                List<T> listMap = resGroupMap.get(key);
                listMap.add(map);
                resGroupMap.put(key, listMap);
            }
        }
        return resGroupMap;
    }

    /**
     * 分组list
     * @param list
     * @param groupKey 分组KEY
     * @param <T>
     * @return 两层list
     */
    public static <T> List<JSONObject> list2GroupList(List<T> list, String groupKey){
        Map<String, List<T>> dataMap =  DataGroupUtil.list2Map(list, groupKey);
        List<JSONObject> resList = new ArrayList<>();
        for (String key : dataMap.keySet()) {
            JSONObject resMap = new JSONObject();
            resMap.put(groupKey, (T)key);
            resMap.put("data", (T)dataMap.get(key));
            resList.add(resMap);
        }
        return resList;
    }

    public static void main(String[] args) {

        List<Account> accounts = new ArrayList<>();
        Account account1 = new Account();
        account1.setCreateMonth("2018-01");
        account1.setAmount(BigDecimal.valueOf(11.11));
        accounts.add(account1);
        Account account2 = new Account();
        account2.setAmount(BigDecimal.valueOf(22.22));
        account2.setCreateMonth("2018-01");
        accounts.add(account2);
        Account account3 = new Account();
        account3.setAmount(BigDecimal.valueOf(33.33));
        account3.setCreateMonth("2018-02");
        accounts.add(account3);

        System.out.println("处理前数据:");
        System.out.println(accounts);

        System.out.println("处理后数据1:");
        System.out.println(DataGroupUtil.list2Map(accounts, "createMonth"));

        System.out.println("处理后数据2:");
        System.out.println(DataGroupUtil.list2GroupList(accounts, "createMonth"));
    }
}```

执行结果:
处理前数据:
[{“amount”:11.11,“createMonth”:“2018-01”},{“amount”:22.22,“createMonth”:“2018-01”},{“amount”:33.33,“createMonth”:“2018-02”}]
处理后数据1:
{2018-01=[{“amount”:11.11,“createMonth”:“2018-01”}, {“amount”:22.22,“createMonth”:“2018-01”}], 2018-02=[{“amount”:33.33,“createMonth”:“2018-02”}]}
处理后数据2:
[{“data”:[{“amount”:11.11,“createMonth”:“2018-01”},{“amount”:22.22,“createMonth”:“2018-01”}],“createMonth”:“2018-01”}, {“data”:[{“amount”:33.33,“createMonth”:“2018-02”}],“createMonth”:“2018-02”}]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值