mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
commit
56e7b1d5c6
@ -1519,4 +1519,30 @@ public class MapUtil {
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将一个Map按照固定大小拆分成多个子Map
|
||||
*
|
||||
* @param map Map
|
||||
* @param size 子Map的大小
|
||||
* @return 子Map列表
|
||||
*/
|
||||
public static <K, V> List<Map<K, V>> partition(Map<K, V> map, int size) {
|
||||
if (map == null) {
|
||||
throw new NullPointerException("Map must not be null");
|
||||
} else if (size <= 0) {
|
||||
throw new IllegalArgumentException("Size must be greater than 0");
|
||||
}
|
||||
List<Map<K, V>> list = new ArrayList<>();
|
||||
Iterator<Map.Entry<K, V>> iterator = map.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map<K, V> subMap = new HashMap<>(size);
|
||||
for (int i = 0; i < size && iterator.hasNext(); i++) {
|
||||
Map.Entry<K, V> entry = iterator.next();
|
||||
subMap.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
list.add(subMap);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user