怎么在Java中實(shí)現(xiàn)一個分頁遍歷功能?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

1. 數(shù)據(jù)查詢模擬
首先mock一個分頁獲取數(shù)據(jù)的邏輯,直接隨機(jī)生成數(shù)據(jù),并且控制最多返回三頁
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;
}針對這種場景,最常見也是最簡單直觀的實(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();
}
}接下來介紹一種更有意思的方式,借助迭代器的遍歷特性來實(shí)現(xiàn),首先自定義一個通用分頁迭代器
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()) {
// 沒有加載到數(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);
}接下來借助上面的迭代器可以比較簡單的實(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);
}
}那么問題來了,上面這種使用方式比前面的優(yōu)勢體現(xiàn)再哪兒呢?
雙層循環(huán)改為單層循環(huán)
接下來接入重點(diǎn)了,在jdk1.8引入了函數(shù)方法 + lambda之后,又提供了一個更簡潔的使用姿勢
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()) {
// 沒有加載到數(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及之后的使用姿勢,一行代碼即可
private static void scanByIteratorInJdk8() {
new MyIterator<>(IteratorTestForJdk18::randStr)
.forEachRemaining(System.out::println);
}看完上述內(nèi)容,你們掌握怎么在Java中實(shí)現(xiàn)一個分頁遍歷功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
分享題目:怎么在Java中實(shí)現(xiàn)一個分頁遍歷功能-創(chuàng)新互聯(lián)
路徑分享:http://www.chinadenli.net/article30/docjso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、小程序開發(fā)、用戶體驗(yàn)、建站公司、App設(shè)計、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容