吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1482|回复: 9
收起左侧

[求助] List<Map<String, Object>>集合中有几十万条数据,每100条为一组取出

[复制链接]
username11 发表于 2021-8-17 10:26
25吾爱币
List<Map<String, Object>> list 会出现几千条数据,从中分组每100条为一组,

在网上找的代码,可是最后却是最后一组覆盖前面的所有,得到的结果就是几百组的结果全是一样的,大佬们应该怎么修改呢?

/**

  • 按照条数对List进行分组
  • home.php?mod=space&uid=952169 list   目标集合
  • @param toIndex  多少条为一组
  • @return
    */
    public static Map groupList(List<Map<String, Object>> list, int toIndex){
    int listSize=list.size();
    Map<String,List<Map<String, Object>>> map = new HashMap();     //用map存起来新的分组后数据
    int keyToken = 0;
    List<Map<String, Object>> result = new ArrayList<>();
    for(int i = 0;i<list.size();i++){
    keyToken++;
    result.add(list.get(i));
    if(keyToken == toIndex){
    //深度复制问题
    //TODO
    map.put(String.valueOf(i),result);
    result.clear();
    keyToken = 0;
    }
    //不足toIndex时最终处理
    if(i == listSize-1 && keyToken != toIndex){
    map.put(String.valueOf(i),result);
    }
    }

    return map;

    }


最佳答案

查看完整内容

[mw_shl_code=java,true] public static Map groupList(List list, int toIndex) { if (list == null || list.isEmpty() || toIndex < 1) { return new HashMap(); } Map result = new HashMap(); int size = list.size(); int count = (size + toIndex - 1) / toIndex; for (int i = 0; i < count; i++) { List subList = list.subLis ...

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

SpeII 发表于 2021-8-17 10:26
[Java] 纯文本查看 复制代码
 public static <T> Map<String, List<T>> groupList(List<T> list, int toIndex) {

        if (list == null || list.isEmpty() || toIndex < 1) {
            return new HashMap<>();
        }

        Map<String, List<T>> result = new HashMap<>();

        int size = list.size();
        int count = (size + toIndex - 1) / toIndex;

        for (int i = 0; i < count; i++) {
            List<T> subList = list.subList(i * toIndex, ((i + 1) * toIndex > size ? size : toIndex * (i + 1)));
            result.put(i * toIndex + "", subList);
        }

        return result;
    }
骑着蚂蚁兜风 发表于 2021-8-17 10:34
 楼主| username11 发表于 2021-8-17 10:37
骑着蚂蚁兜风 发表于 2021-8-17 10:39
username11 发表于 2021-8-17 10:37
大佬能细致一点吗?菜鸡的我不懂....

百度吧  大把的资料
 楼主| username11 发表于 2021-8-17 11:01
SpeII 发表于 2021-8-17 10:44
[mw_shl_code=java,true] public static  Map groupList(List list, int toIndex) {

        if (list = ...

膜拜大佬,强!!
 楼主| username11 发表于 2021-8-17 11:06
harmonycc 发表于 2021-8-17 11:04
result.clear()应该改成new吧

已经有大佬解决了 非常感谢
Lianda 发表于 2021-8-17 20:22
感觉用桶来性能会更好点
杨辣子 发表于 2021-8-21 16:28
result.clear();
问题出在这里
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-6 17:04

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表