有时候我们需要分批处理数据,1个List集合包含上万条数据,每次处理5000条则需要分割,封装1个方法进行处理。
/** * 按指定大小,分隔集合为N个部分 * * @param list 原集合,为空时则返回空集合 * @param length 指定的大小 * @return */ public static <T> List<List<T>> splitList(List<T> list, int length) { // 返回结果 List<List<T>> result = new ArrayList<>(); // 为空则返回空 if (list == null || list.isEmpty()) { return result; } // 传入集合长度 int size = list.size(); // 如果传入集合长度小于指定长度,直接返回 if (size <= length) { result.add(list); return result; } // 分隔后的集合个数 int count = (size + length - 1) / length; for (int i = 0; i < count; i++) { List<T> subList = list.subList(i * length, ((i + 1) * length > size ? size : length * (i + 1))); result.add(subList); } return result; }
调用例子:
// 每次分割1000条 List<List<String>> styleIdSplit = ListUtil.splitList(styleIds, 1000);
字节(Byte)是计量单位,表示数据量多少,是计算机信息技术用于计量存储容量的一种计量单位,通常情况下一字节等于八位。字符(Character)计算机中使用的字母、数字、字和符号,比如'A'、'B'、'$'、'&'等。一般在英文...
public class test { public static void main(String[] args) { &...
在java中字符串属于对象,刚开始我就疑惑为什么int char等类型都是小写,结果String是大写,java太反人类,后来才知道String是对象。(1).java创建字符串String text = "net"; String tex...
java stringBuffer(1).stringBuffer和stringBuilder区别stringBuffer是线程安全的,stringBuilder速度更快(2).简单的stringBuffer例子StringBuffer sBuffer = new&nb...
(1).java获取当前日期时间Date date = new Date(); System.out.println(date.toString());输出Fri Jul 02 10:29:55 CST 2021(2).java获取时间戳秒/毫秒D...
(1).java睡眠函数Thread.sleep(时间); //单位为毫秒(2).java睡眠函数例子Date dNow = new Date(); SimpleDateFormat ft = new&nbs...