天天看點

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

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());      

繼續閱讀