Java 實現(xiàn)將List平均分成若干個集合
1.初衷是由于調(diào)用銀行接口的批量處理接口時,每次最多只能處理500條數(shù)據(jù),但是當(dāng)數(shù)據(jù)總數(shù)為510條時。我又不想第一次調(diào)用處理500條,第二次調(diào)用處理10條數(shù)據(jù),我想要的是每次處理255條數(shù)據(jù)。
下面展示的是我的處理方法
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,主要通過偏移量來實現(xiàn)的 * * @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; // (先計算出余數(shù)) 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 實現(xiàn)不平均分組 result = Lists.partition(list ,100);result.forEach(l -> { l.forEach(i -> System.out.print(i + 't') ); System.out.println(); }); }}
3.展示一下測試結(jié)果:
補充知識:Java8 Lambda 分割List
我就廢話不多說了,大家還是直接看代碼吧~
/** * @author caishen * @version 1.0 * @className CollectionUtils * @date 2019/5/23 11:54 * 自分で??い駿暢`ドの各行を擔(dān)當(dāng)する * @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)); }}
以上這篇Java 實現(xiàn)將List平均分成若干個集合就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實現(xiàn)代碼2. Ajax實現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)3. jsp EL表達(dá)式詳解4. jsp中sitemesh修改tagRule技術(shù)分享5. JavaWeb Servlet中url-pattern的使用6. 爬取今日頭條Ajax請求7. 如何使用瀏覽器擴展篡改網(wǎng)頁中的JS 文件8. ASP基礎(chǔ)知識VBScript基本元素講解9. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)10. JSP servlet實現(xiàn)文件上傳下載和刪除
