Java 實(shí)現(xiàn)將List平均分成若干個(gè)集合
1.初衷是由于調(diào)用銀行接口的批量處理接口時(shí),每次最多只能處理500條數(shù)據(jù),但是當(dāng)數(shù)據(jù)總數(shù)為510條時(shí)。我又不想第一次調(diào)用處理500條,第二次調(diào)用處理10條數(shù)據(jù),我想要的是每次處理255條數(shù)據(jù)。
下面展示的是我的處理方法
2.寫了一個(gè)簡(jiǎn)單的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 { /** * 將一個(gè)List均分成n個(gè)list,主要通過(guò)偏移量來(lái)實(shí)現(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; // (先計(jì)算出余數(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 實(shí)現(xiàn)不平均分組 result = Lists.partition(list ,100);result.forEach(l -> { l.forEach(i -> System.out.print(i + 't') ); System.out.println(); }); }}
3.展示一下測(cè)試結(jié)果:
補(bǔ)充知識(shí):Java8 Lambda 分割List
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
/** * @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 實(shí)現(xiàn)將List平均分成若干個(gè)集合就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 用xslt+css讓RSS顯示的跟網(wǎng)頁(yè)一樣漂亮2. ASP.NET MVC把數(shù)據(jù)庫(kù)中枚舉項(xiàng)的數(shù)字轉(zhuǎn)換成文字3. 《CSS3實(shí)戰(zhàn)》筆記--漸變?cè)O(shè)計(jì)(一)4. 測(cè)試模式 - XSL教程 - 55. Ajax實(shí)現(xiàn)異步加載數(shù)據(jù)6. 教你JS更簡(jiǎn)單的獲取表單中數(shù)據(jù)(formdata)7. ASP.NET Core自定義中間件的方式詳解8. html5手機(jī)觸屏touch事件介紹9. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效10. 讓chatgpt將html中的圖片轉(zhuǎn)為base64方法示例
