集合工具类

集合工具类


import com.google.common.collect.Lists;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author windbird
 * @Date 2023/4/26 16:25
 * @ClassName ListLimitUtils
 * @desc: 集合分批处理
 */
public class ListUtil {
    private Integer listPage = 0;
    private Integer listPageSize = 10;

    public ListUtil() {
    }

    public ListUtil(Integer listPageSize) {
        this.listPageSize = listPageSize;
    }

    /**
     * 对集合进行批处理 防止当前集合过大
     *  List NameList = Arrays.asList("张三", "李四","王五", "中农", "公三", "sugar", "red", "wind", "oso", "兴业","zh");
     *  ["张三","李四","王五"]
     * ["中农","公三","sugar"]
     * ["red","wind","oso"]
     * ["兴业","zh"]
     * @example:
     *   for(List resultList:resultBatchList){
     *       createBatch(resultList)
     *   }
     *
     * @param sourceList
     * @return
     */
    public  List> getInputListToBatches(List sourceList){
        List> resultBatchList =  new ArrayList<>(8);
        if(!CollectionUtils.isEmpty(sourceList)){
           int inputListSize = sourceList.size();
           if(inputListSize<=listPageSize){
               resultBatchList.add(sourceList);
            }else{
               listPage= inputListSize % listPageSize==0 ? inputListSize / listPageSize : inputListSize / listPageSize+1;
               for (int i = 0; i  subListPageStream(List sourceList, int page, int pageSize) {
        if(!CollectionUtils.isEmpty(sourceList)){
            int count = sourceList.size();
            int totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
            int startIndex = page<=0?0:(page>totalPage?(totalPage-1)*pageSize :(page-1)*pageSize);
            return sourceList.stream().skip(startIndex).limit(pageSize).collect(Collectors.toList());
        }
       return new ArrayList<>();
    }

    /**
     * @desc  对集合进行分页
     * @param sourceList
     * @param page
     * @param pageSize
     * @return
     */
    public List subListPage(List sourceList, int page, int pageSize) {
        if(!CollectionUtils.isEmpty(sourceList)){
            int count = sourceList.size();
            int totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
            int startIndex = page<=0?0:(page>totalPage?(totalPage-1)*pageSize :(page-1)*pageSize);
            return sourceList.subList(startIndex,Math.min(startIndex+pageSize,count));
        }
       return new ArrayList<>();
    }

    /**
     * 对集合分页
     * @param sourceList
     * @param page
     * @param pageSize
     * @return
     */
    public List partitionPage(List sourceList,int page, int pageSize) {
        List> partition = Lists.partition(sourceList, pageSize);
        int pages = partition.size();
        page = page <= 0 ? 0 : (page <= (pages - 1) ? page : (pages - 1));
        return partition.get(page);
    }

}


            com.google.guava
            guava
            28.0-jre
        
`java


你可能感兴趣的:(java)