软件编程
位置:首页>> 软件编程>> java编程>> Java 实现将List平均分成若干个集合

Java 实现将List平均分成若干个集合

作者:after95  发布时间:2023-10-16 21:36:59 

标签:Java,List,平均,集合

1.初衷是由于调用银行接口的批量处理接口时,每次最多只能处理500条数据,但是当数据总数为510条时。我又不想第一次调用处理500条,第二次调用处理10条数据,我想要的是每次处理255条数据。

下面展示的是我的处理方法

2.写了一个简单的ListUtils:


package com.example.springboottest.common.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.common.collect.Lists;
/**
* List 工具类
* @author Neo
* @date 2018年4月16日13:13:37
*/
public class ListUtils {

/**
  * 将一个List均分成n个list,主要通过偏移量来实现的
  *
  * @param source 源集合
  * @param limit 最大值
  * @return
  */
 public static <T> List<List<T>> averageAssign(List<T> source, int limit) {
   if (null == source || source.isEmpty()) {
     return Collections.emptyList();
   }
   List<List<T>> result = new ArrayList<>();
   int listCount = (source.size() - 1) / limit + 1;
   int remaider = source.size() % listCount; // (先计算出余数)
   int number = source.size() / listCount; // 然后是商
   int offset = 0;// 偏移量
   for (int i = 0; i < listCount; i++) {
     List<T> value;
     if (remaider > 0) {
       value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
       remaider--;
       offset++;
     } else {
       value = source.subList(i * number + offset, (i + 1) * number + offset);
     }
     result.add(value);
   }
   return result;
 }
 public static void main(String[] args) {
   List list = new ArrayList();
   for (int i = 0; i < 65; i++) {
     list.add(i);
   }
   List<List> result = averageAssign(list, 15);
   result.forEach(l -> {
     l.forEach(i ->
         System.out.print(i + "\t")
     );
     System.out.println();
   });
   System.out.println("====================================================");
   result = averageAssign(list, 20);
   result.forEach(l -> {
     l.forEach(i ->
         System.out.print(i + "\t")
     );
     System.out.println();
   });

System.out.println("====================================================");
   // Guava 实现不平均分组
   result = Lists.partition(list ,100);
       result.forEach(l -> {
     l.forEach(i ->
         System.out.print(i + "\t")
     );
     System.out.println();
   });
 }
}

3.展示一下测试结果:

Java 实现将List平均分成若干个集合

补充知识:Java8 Lambda 分割List

我就废话不多说了,大家还是直接看代码吧~


/**
* @author caishen
* @version 1.0
* @className CollectionUtils
* @date 2019/5/23 11:54
* 自分で書いたコードの各行を担当する
* @dis 切割list工具类
**/
public class CollectionUtils {

public static <T> List<List<T>> divide(List<T>origin , int size){
   if(Assert.isEmpty(origin)){
     return Collections.emptyList();
   }

int block = (origin.size() + size -1) / size;
   return IntStream.range(0,block).
       boxed().map(i->{
         int start = i*size;
         int end = Math.min(start + size,origin.size());
         return origin.subList(start,end);
   }).collect(Collectors.toList());
 }

public static void main(String[] args) {
   System.out.println(divide(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 3));
 }
}

来源:https://blog.csdn.net/after95/article/details/79959274

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com