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é)果也會(huì)隨著改變。
@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)); }
- 沒有對(duì)應(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的改變不會(huì)影響分組后的結(jié)果。
2partitioningBy 其實(shí)是一種特殊的 groupingBy,它依照條件測(cè)試的是否兩種結(jié)果來(lái)構(gòu)造返回的數(shù)據(jù)結(jié)構(gòu),get(true) 和 get(false) 能即為全部的元素對(duì)象。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中的ApplicationRunner與CommandLineRunner問題
這篇文章主要介紹了SpringBoot中的ApplicationRunner與CommandLineRunner問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09SpringBoot Application注解原理及代碼詳解
這篇文章主要介紹了SpringBoot Application注解原理及代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06mybatisplus邏輯刪除基本實(shí)現(xiàn)和坑點(diǎn)解決
這篇文章主要介紹了mybatisplus邏輯刪除基本實(shí)現(xiàn)和坑點(diǎn)解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03減小Maven項(xiàng)目生成的JAR包體積實(shí)現(xiàn)提升運(yùn)維效率
在Maven構(gòu)建Java項(xiàng)目過程中,減小JAR包體積可通過排除不必要的依賴和使依賴jar包獨(dú)立于應(yīng)用jar包來(lái)實(shí)現(xiàn),在pom.xml文件中使用<exclusions>標(biāo)簽排除不需要的依賴,有助于顯著降低JAR包大小,此外,將依賴打包到應(yīng)用外,可減少應(yīng)用包的體積2024-10-10java版微信公眾平臺(tái)消息接口應(yīng)用示例
這篇文章主要介紹了java版微信公眾平臺(tái)消息接口應(yīng)用,結(jié)合實(shí)例形式對(duì)比分析了PHP與java應(yīng)用微信公眾平臺(tái)接口的相關(guān)調(diào)用與操作技巧,需要的朋友可以參考下2017-07-07springBoot 創(chuàng)建定時(shí)任務(wù)過程詳解
這篇文章主要介紹了springBoot 創(chuàng)建定時(shí)任務(wù)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10java利用java.net.URLConnection發(fā)送HTTP請(qǐng)求的方法詳解
如何通過Java(模擬瀏覽器)發(fā)送HTTP請(qǐng)求是我們?cè)谌粘=?jīng)常會(huì)遇到的問題,下面這篇文章主要給大家介紹了關(guān)于java利用java.net.URLConnection發(fā)送HTTP請(qǐng)求的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-05-05