Commit ff8da1b7 by tc

feature:[none]增加list分组方法

parent 3cd6b662
afafa
\ No newline at end of file
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
*/ */
package com.pcloud.common.utils; package com.pcloud.common.utils;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
...@@ -16,5 +17,55 @@ public class ListUtils { ...@@ -16,5 +17,55 @@ public class ListUtils {
public static boolean isEmpty(List<?> list) { public static boolean isEmpty(List<?> list) {
return (list == null || list.isEmpty()); return (list == null || list.isEmpty());
} }
private static final Integer OFFSET = 500;
/**
* 切割list,默认500长度一个
* @param list
* @param <T>
* @return
*/
public static <T> List<List<T>> groupList(List<T> list) {
return groupList(list,OFFSET);
}
/**
* 切割list,自定义长度
* @param list
* @param length
* @param <T>
* @return
*/
public static <T> List<List<T>> groupList(List<T> list,int length) {
List<List<T>> listGroup = new ArrayList<>();
int listSize = list.size();
//子集合的长度
int toIndex = length;
for (int i = 0; i < list.size(); i += length) {
if (i + length > listSize) {
toIndex = listSize - i;
}
List<T> newList = list.subList(i, i + toIndex);
listGroup.add(newList);
}
return listGroup;
}
/**
* 切割list,自定义子集合个数
* @param list
* @param <T>
* @return
*/
public static <T> List<List<T>> groupListBySize(List<T> list,int size) {
int length = list.size()/size;
if(list.size()<10){
length = 1;
}
return groupList(list,length);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment