编程语言
首页 > 编程语言> > Java 将map 拆分成多个map工具类

Java 将map 拆分成多个map工具类

作者:互联网

转自 https://blog.csdn.net/q1534894305/article/details/108364758

 

开箱即用,无副作用

/**
* 将map 拆分成多个map
*
* @param chunkMap 被拆的 map
* @param chunkNum 每段的大小
* @param <k> map 的 key类 型
* @param <v> map 的value 类型
* @return List
*/
public static <k, v> List<LinkedHashMap<k, v>> mapChunk(LinkedHashMap<k, v> chunkMap, int chunkNum) {
if (chunkMap == null || chunkNum <= 0) {
List<LinkedHashMap<k, v>> list = new ArrayList<>();
list.add(chunkMap);
return list;
}
Set<k> keySet = chunkMap.keySet();
Iterator<k> iterator = keySet.iterator();
int i = 1;
List<LinkedHashMap<k, v>> total = new ArrayList<>();
LinkedHashMap<k, v> tem = new LinkedHashMap<>();
while (iterator.hasNext()) {
k next = iterator.next();
tem.put(next, chunkMap.get(next));
if (i == chunkNum) {
total.add(tem);
tem = new LinkedHashMap<>();
i = 0;
}
i++;
}
if (!CollectionUtils.isEmpty(tem)) {
total.add(tem);
}
return total;
}

  

标签:分成,map,total,Java,tem,iterator,chunkMap,LinkedHashMap
来源: https://www.cnblogs.com/mimimikasa/p/16063802.html