Commit 824e5074 by 田超

Merge branch 'feature/listUtil' into 'master'

Feature/list util

See merge request rays/pcloud-common-parent!163
parents 4d833466 86ae8468
afafa
\ No newline at end of file
package com.pcloud.common.utils;
import java.util.List;
public interface ListForInSQL<T> {
<T>List<T>processSQL(List<Long> IdList);
}
...@@ -3,6 +3,10 @@ ...@@ -3,6 +3,10 @@
*/ */
package com.pcloud.common.utils; package com.pcloud.common.utils;
import org.apache.poi.ss.formula.functions.T;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
/** /**
...@@ -16,5 +20,74 @@ public class ListUtils { ...@@ -16,5 +20,74 @@ 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);
}
public static <T> List<T>groupList4SQL(ListForInSQL<T> listForInSQL,List<Long>idList){
HashSet<Long>idSet = new HashSet<>(idList);
idList.clear();
idList.addAll(idSet);
List<T>result = new ArrayList<>();
if(idList!=null && idList.size()>500){
List<List<Long>> lists = groupList(idList);
for (List<Long> list : lists) {
List<T> tempList = listForInSQL.processSQL(list);
result.addAll(tempList);
}
return result;
}else if(idList != null && idList.size()>0){
return listForInSQL.processSQL(idList);
}else{
return new ArrayList<>();
}
}
} }
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