久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Java util.List如何實(shí)現(xiàn)列表分段處理

瀏覽:104日期:2022-08-23 17:18:34

java.util.List 分段

使用google的guava類庫對List分段處理

List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);List<List<Integer>> subSets = Lists.partition(intList, 3);List<Integer> last = subSets.get(2);

原理是內(nèi)部封裝著我們要分段的List的引用,在subSets.get(index) 語句時(shí),對參數(shù)List.subList()動(dòng)態(tài)處理

對集合的處理

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

使用iterable進(jìn)行遍歷,iterator.next()方法,內(nèi)部是使用固定size大小的數(shù)組循環(huán)狀態(tài)size次數(shù)據(jù),然后返回?cái)?shù)據(jù)

使用apache common工具的的List分段處理方法

ArrayList<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);List<List<Integer>> subSets2 = ListUtils.partition(intList, 3);

這個(gè)方法和guava包的列表分段方法原理是相同的

自定義泛型方法進(jìn)行分頁

/** * 列表數(shù)據(jù)分組 * @param source 源數(shù)據(jù) * @param size 根據(jù)大小分組 * @param <T> 泛型 * @return */ public static <T> List<List<T>> averageAssign(List<T> source, int size){ List<List<T>> result = new ArrayList<>(); int offset=0; boolean isZero = source.size()%size==0; int totalPage = source.size()/size + 1; int totalSize = source.size(); while(totalPage-1>=offset){ List<T> subList = null; if(offset == totalPage-1){if(isZero){ break;}//最后一段的處理subList = source.subList(size * offset, totalSize); }else{subList = source.subList(size * offset, size * (offset + 1)); } offset++; result.add(subList); } return result; }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 若尔盖县| 乐陵市| 新竹县| 漳平市| 西峡县| 新余市| 锡林郭勒盟| 洛南县| 剑阁县| 乌鲁木齐县| 安塞县| 龙南县| 古交市| 上虞市| 繁峙县| 逊克县| 雅安市| 兰州市| 景东| 通山县| 新昌县| 革吉县| 石首市| 二连浩特市| 十堰市| 永德县| 常德市| 茌平县| 藁城市| 科尔| 桐城市| 益阳市| 延庆县| 台湾省| 赤水市| 杨浦区| 辛集市| 集贤县| 万载县| 临安市| 武川县|