动态截取集合字符串算法

来源:互联网 发布:软件离职项目交接 编辑:程序博客网 时间:2024/06/04 19:17

以下内容只为记录:

有时需要将长度不确定的集合中数据动态的获取到一定长度的集合进行相应的操作. 这是可以将集合转为map进行操作, 以下为代码片段


int QUERYNUM = 30;
Map<Integer, List<Long>> resultMap = new TreeMap<>();int size = resultList.size();// 获取到集合list的长度
// 转为mapfor (int i = 0; i < size; i++) {    int tem = i / QUERYNUM;// 需要获取到一定的长度    List<Long> tempList = resultMap.get(tem);    if (tempList == null) {        tempList = new ArrayList<Long>();    }    tempList.add(resultList.get(i));    resultMap.put(tem, tempList);}// 循环遍历获取商品详情for (List<Long> resList : resultMap.values()) {    }