国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

詳解Java中兩種分頁(yè)遍歷的使用姿勢(shì)

瀏覽:4日期:2022-08-15 18:26:32

在日常開(kāi)發(fā)中,分頁(yè)遍歷迭代的場(chǎng)景可以說(shuō)非常普遍了,比如掃表,每次撈100條數(shù)據(jù),然后遍歷這100條數(shù)據(jù),依次執(zhí)行某個(gè)業(yè)務(wù)邏輯;這100條執(zhí)行完畢之后,再加載下一百條數(shù)據(jù),直到掃描完畢

那么要實(shí)現(xiàn)上面這種分頁(yè)迭代遍歷的場(chǎng)景,我們可以怎么做呢

本文將介紹兩種使用姿勢(shì)

常規(guī)的使用方法 借助Iterator的使用姿勢(shì)1. 數(shù)據(jù)查詢(xún)模擬

首先mock一個(gè)分頁(yè)獲取數(shù)據(jù)的邏輯,直接隨機(jī)生成數(shù)據(jù),并且控制最多返回三頁(yè)

public static int cnt = 0;private static List<String> randStr(int start, int size) { ++cnt; if (cnt > 3) { return Collections.emptyList(); } else if (cnt == 3) { cnt = 0; size -= 2; } System.out.println('======================= start to gen randList ===================='); List<String> ans = new ArrayList<>(size); for (int i = 0; i < size; i++) { ans.add((start + i) + '_' + UUID.randomUUID().toString()); } return ans;}2. 基本實(shí)現(xiàn)方式

針對(duì)這種場(chǎng)景,最常見(jiàn)也是最簡(jiǎn)單直觀的實(shí)現(xiàn)方式

while死循環(huán) 內(nèi)部遍歷

private static void scanByNormal() { int start = 0; int size = 5; while (true) { List<String> list = randStr(start, size); for (String str : list) { System.out.println(str); } if (list.size() < size) { break; } start += list.size(); }}3. 迭代器實(shí)現(xiàn)方式

接下來(lái)介紹一種更有意思的方式,借助迭代器的遍歷特性來(lái)實(shí)現(xiàn),首先自定義一個(gè)通用分頁(yè)迭代器

public static abstract class MyIterator<T> implements Iterator<T> { private int start = 0; private int size = 5; private int currentIndex; private boolean hasMore = true; private List<T> list; public MyIterator() { } @Override public boolean hasNext() { if (list != null && list.size() > currentIndex) { return true; } // 當(dāng)前的數(shù)據(jù)已經(jīng)加載完畢,嘗試加載下一批 if (!hasMore) { return false; } list = load(start, size); if (list == null || list.isEmpty()) { // 沒(méi)有加載到數(shù)據(jù),結(jié)束 return false; } if (list.size() < size) { // 返回條數(shù)小于限制條數(shù),表示還有更多的數(shù)據(jù)可以加載 hasMore = false; } currentIndex = 0; start += list.size(); return true; } @Override public T next() { return list.get(currentIndex++); } public abstract List<T> load(int start, int size);}

接下來(lái)借助上面的迭代器可以比較簡(jiǎn)單的實(shí)現(xiàn)我們的需求了

private static void scanByIterator() { MyIterator<String> iterator = new MyIterator<String>() { @Override public List<String> load(int start, int size) { return randStr(start, size); } }; while (iterator.hasNext()) { String str = iterator.next(); System.out.println(str); }}

那么問(wèn)題來(lái)了,上面這種使用方式比前面的優(yōu)勢(shì)體現(xiàn)再哪兒呢?

雙層循環(huán)改為單層循環(huán)

接下來(lái)接入重點(diǎn)了,在jdk1.8引入了函數(shù)方法 + lambda之后,又提供了一個(gè)更簡(jiǎn)潔的使用姿勢(shì)

public class IteratorTestForJdk18 { @FunctionalInterface public interface LoadFunc<T> { List<T> load(int start, int size); } public static class MyIterator<T> implements Iterator<T> { private int start = 0; private int size = 5; private int currentIndex; private boolean hasMore = true; private List<T> list; private LoadFunc<T> loadFunc; public MyIterator(LoadFunc<T> loadFunc) { this.loadFunc = loadFunc; } @Override public boolean hasNext() { if (list != null && list.size() > currentIndex) {return true; } // 當(dāng)前的數(shù)據(jù)已經(jīng)加載完畢,嘗試加載下一批 if (!hasMore) {return false; } list = loadFunc.load(start, size); if (list == null || list.isEmpty()) {// 沒(méi)有加載到數(shù)據(jù),結(jié)束return false; } if (list.size() < size) {// 返回條數(shù)小于限制條數(shù),表示還有更多的數(shù)據(jù)可以加載hasMore = false; } currentIndex = 0; start += list.size(); return true; } @Override public T next() { return list.get(currentIndex++); } }}

在jdk1.8及之后的使用姿勢(shì),一行代碼即可

private static void scanByIteratorInJdk8() { new MyIterator<>(IteratorTestForJdk18::randStr) .forEachRemaining(System.out::println);}

這次對(duì)比效果是不是非常顯眼了,從此以后分頁(yè)迭代遍歷再也不用冗長(zhǎng)的雙重迭代了

到此這篇關(guān)于詳解Java中兩種分頁(yè)遍歷的使用姿勢(shì)的文章就介紹到這了,更多相關(guān)Java 分頁(yè)遍歷內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 91国内精品久久久久影院优播 | 特黄特黄aaaa级毛片免费看 | 激情欧美一区二区三区 | 一级特黄色毛片免费看 | 国产精品免费aⅴ片在线观看 | 日本亚洲综合 | 日本久久香蕉一本一道 | 亚洲三级大片 | 欧美三级不卡在线观看视频 | 亚洲精品一区二区久久这里 | 免费永久国产在线视频 | 欧美成人片在线 | 八戒午夜精品视频在线观看 | 一级a爰片久久毛片 | 美女视频黄a全部免费专区一 | 国产精品久久久 | 欧美色欧美亚洲高清在线视频 | 亚洲人成网站在线观看播放 | 国产呦精品系列在线 | 久久综合久久88 | 一本三道a无线码一区v小说 | 国产成人最新毛片基地 | 日本三级香港三级少妇 | 亚洲免费视频在线观看 | 91在线免费观看网站 | 呦女精品 | 2022免费国产精品福利在线 | 国产成人99精品免费观看 | 亚洲 欧美 激情 另类 自拍 | 国内精品久久久久久久久久影视 | 亚洲欧美日韩国产一区二区精品 | 久久欧美成人精品丝袜 | www.黄网站| 日韩一区二区在线播放 | 中文字幕一区二区在线视频 | 一区二区三区在线 | 欧美激情一级欧美精品 | 国产成人免费全部网站 | 手机看片在线 | 国产成人精品视频免费大全 | 一级黄色片aaa |