java如何將list按照指定數(shù)量分成小list
java將list按照指定數(shù)量分成小list
自己編寫相關(guān)代碼:
使用guava
import java.util.ArrayList;
import java.util.List;
import com.google.common.collect.Lists;
public class Test4 {
public static void main(String[] args) {
List<Long> list = new ArrayList<>();
list.add(1L);
list.add(2L);
list.add(3L);
list.add(4L);
list.add(5L);
list.add(6L);
list.add(7L);
list.add(8L);
list.add(9L);
//test1(list);
test2(list);
}
private static void test1(List<Long> list) {
int size = 2;
List<List<Long>> listArr = new ArrayList<>();
int arrSize = list.size()%size==0?list.size()/size:list.size()/size+1;
for(int i=0;i<arrSize;i++) {
List<Long> sub = new ArrayList<>();
for(int j=i*size;j<=size*(i+1)-1;j++) {
if(j<=list.size()-1) {
sub.add(list.get(j));
}
}
listArr.add(sub);
}
System.out.println(listArr.toString());
}
private static void test2(List<Long> list) {
int size = 16;
List<List<Long>> subSets = Lists.partition(list, size);
System.out.println(subSets.toString());
}
} 補(bǔ)充:guava分割其他collectionsiew plain
@Test
public void givenCollection_whenParitioningIntoNSublists_thenCorrect() {
Collection<Integer> intCollection = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
Iterable<List<Integer>> subSets = Iterables.partition(intCollection, 3);
List<Integer> firstPartition = subSets.iterator().next();
List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(1, 2, 3);
assertThat(firstPartition, equalTo(expectedLastPartition));
} 以上需要注意的是:
partition返回的是原list的subview.視圖,也即,原list改變后,partition之后的結(jié)果也會隨著改變。
@Test
public void givenListPartitioned_whenOriginalListIsModified_thenPartitionsChangeAsWell() {
// Given
List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
List<List<Integer>> subSets = Lists.partition(intList, 3);
// When
intList.add(9);
// Then
List<Integer> lastPartition = subSets.get(2);
List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8, 9);
assertThat(lastPartition, equalTo(expectedLastPartition));
} 使用apache commons collection
@Test
public void givenList_whenParitioningIntoNSublists_thenCorrect() {
List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
List<List<Integer>> subSets = ListUtils.partition(intList, 3);
List<Integer> lastPartition = subSets.get(2);
List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
assertThat(subSets.size(), equalTo(3));
assertThat(lastPartition, equalTo(expectedLastPartition));
} - 沒有對應(yīng)的Iterable.partions方法,類似guava那樣
- partition后的結(jié)果同樣是原集合的視圖。
Java8方法
1)通過grouping by:
@Test
public final void givenList_whenParitioningIntoNSublistsUsingGroupingBy_thenCorrect() {
List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
Map<Integer, List<Integer>> groups =
intList.stream().collect(Collectors.groupingBy(s -> (s - 1) / 3));
List<List<Integer>> subSets = new ArrayList<List<Integer>>(groups.values());
List<Integer> lastPartition = subSets.get(2);
List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
assertThat(subSets.size(), equalTo(3));
assertThat(lastPartition, equalTo(expectedLastPartition));
} 按年齡分組:
Map<Integer, List<Person>> personGroups = Stream.generate(new PersonSupplier()).
limit(100).
collect(Collectors.groupingBy(Person::getAge));
Iterator it = personGroups.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, List<Person>> persons = (Map.Entry) it.next();
System.out.println("Age " + persons.getKey() + " = " + persons.getValue().size());
} 2)通過partition by:
@Test
public void givenList_whenParitioningIntoSublistsUsingPartitionBy_thenCorrect() {
List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
Map<Boolean, List<Integer>> groups =
intList.stream().collect(Collectors.partitioningBy(s -> s > 6));
List<List<Integer>> subSets = new ArrayList<List<Integer>>(groups.values());
List<Integer> lastPartition = subSets.get(1);
List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
assertThat(subSets.size(), equalTo(2));
assertThat(lastPartition, equalTo(expectedLastPartition));
} 按照成年未成年人分組:
Map<Boolean, List<Person>> children = Stream.generate(new PersonSupplier()).
limit(100).
collect(Collectors.partitioningBy(p -> p.getAge() < 18));
System.out.println("Children number: " + children.get(true).size());
System.out.println("Adult number: " + children.get(false).size()); 注意:
1.Java8方式,分組后的list不再是原list的視圖。所以,原list的改變不會影響分組后的結(jié)果。
2partitioningBy 其實(shí)是一種特殊的 groupingBy,它依照條件測試的是否兩種結(jié)果來構(gòu)造返回的數(shù)據(jù)結(jié)構(gòu),get(true) 和 get(false) 能即為全部的元素對象。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA實(shí)現(xiàn)按時間段查詢數(shù)據(jù)操作
這篇文章主要介紹了JAVA實(shí)現(xiàn)按時間段查詢數(shù)據(jù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Map按單個或多個Value排序當(dāng)Value相同時按Key排序
Map可以先按照value進(jìn)行排序,然后按照key進(jìn)行排序。 或者先按照key進(jìn)行排序,然后按照value進(jìn)行排序,這樣操作都行,這篇文章主要介紹了Map按單個或多個Value排序,當(dāng)Value相同時按Key排序,需要的朋友可以參考下2023-02-02
詳解Elasticsearch如何把一個索引變?yōu)橹蛔x
這篇文章主要為大家介紹了詳解Elasticsearch如何把一個索引變?yōu)橹蛔x示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
詳解SpringMVC組件之HandlerMapping(二)
這篇文章主要介紹了詳解SpringMVC組件之HandlerMapping(二),HandlerMapping組件是Spring?MVC核心組件,用來根據(jù)請求的request查找對應(yīng)的Handler,在Spring?MVC中,有各式各樣的Web請求,每個請求都需要一個對應(yīng)的Handler來處理,需要的朋友可以參考下2023-08-08
Java中枚舉的實(shí)現(xiàn)與應(yīng)用詳解
這篇文章主要介紹了Java中枚舉的實(shí)現(xiàn)與應(yīng)用詳解,EnumTest中還有一個VALUES數(shù)組,里面存儲著所有的枚舉實(shí)例,調(diào)用values方法時返回VALUES數(shù)組的clone,需要的朋友可以參考下2023-12-12
Java Socket編程心跳包創(chuàng)建實(shí)例解析
這篇文章主要介紹了Java Socket編程心跳包創(chuàng)建實(shí)例解析,具有一定借鑒價值,需要的朋友可以參考下2017-12-12
Java for-each循環(huán)(遍歷循環(huán))詳解
本文詳解Java for-each循環(huán),涵蓋其簡化遍歷、類型安全、不可變性、空指針防護(hù)等特性,適用場景及限制,錯誤案例與修正,性能對比,高級技巧(如Java14 Records),并提供最佳實(shí)踐指南2025-07-07

