java 将list按照指定数量分成小list

本文介绍如何使用不同方法将Java列表分割成子列表,包括自定义实现、Guava库、Apache Commons Collections库及Java 8的新特性。这些方法适用于各种场景,并提供了灵活的选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、自己编写相关代码:

2、使用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());  
    }  
}  

 

补充: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之后的结果也会随着改变。

@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));    
}    


3、使用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));    
}   

1.没有对应的Iterable.partions方法,类似guava那样

 

2.partition后的结果同样是原集合的视图。

 

4、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的改变不会影响分组后的结果。
2partitioningBy 其实是一种特殊的 groupingBy,它依照条件测试的是否两种结果来构造返回的数据结构,get(true) 和 get(false) 能即为全部的元素对象。
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值