南京做网站xjrkj,桦甸网站建设,软件商店安装下载2023,上海市建设小学网站文章目录 1. 场景2. 拆分集合方法#xff08;写了三种#xff09;3. 格式化打印方法 1. 场景
在数据库批量操作时#xff0c;有可能数据量过大#xff0c;不能一次性操作#xff0c;所以需要将大集合拆分为多个小集合进行多次操作
2. 拆分集合方法#xff08;写了三种写了三种3. 格式化打印方法 1. 场景
在数据库批量操作时有可能数据量过大不能一次性操作所以需要将大集合拆分为多个小集合进行多次操作
2. 拆分集合方法写了三种
import com.google.common.collect.Lists;
import com.xin.demo.fastjsondemo.FastJsonFormatPrintUtils;
import org.apache.commons.collections4.CollectionUtils;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;public class PartitionListDemo {private static final int PARTITION_SIZE 10;public static void main(String[] args) {// 大集合ListString list new ArrayList();for (int i 0; i 33; i) {list.add(xinliushijian i);}// 方法1 guava工具ListListString partitionList Lists.partition(list, PARTITION_SIZE);FastJsonFormatPrintUtils.formatPrint(partitionList);System.out.println();// 方法2 stream流实现ListListString partitionList1 partList1(list, PARTITION_SIZE);FastJsonFormatPrintUtils.formatPrint(partitionList1);// 方法3 普通方法实现partList2(list, PARTITION_SIZE);}public static T ListListT partList1(ListT list, int size) {if (CollectionUtils.isEmpty(list) || size 1) {return new ArrayList();}ListListT partList IntStream.range(0, list.size()).boxed().collect(Collectors.groupingBy(index - index / size)).values().stream().map(indices - indices.stream().map(list::get).collect(Collectors.toList())).collect(Collectors.toList());return partList;}public static T ListListT partList2(ListT list, int size) {if (CollectionUtils.isEmpty(list) || size 1) {return new ArrayList();}ListListT allPartList new ArrayList();for (int i 0; i list.size(); isize) {if (i size list.size()) {size list.size() - i;}ListT parList list.subList(i, i size);allPartList.add(parList);}FastJsonFormatPrintUtils.formatPrint(allPartList);return allPartList;}}
3. 格式化打印方法
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.JSON;public class FastJsonFormatPrintUtils {public static void formatPrint(Object object) {String pretty JSON.toJSONString(object, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteDateUseDateFormat);System.out.println(pretty);}}