本文介紹了是否可以在 Java 中合并迭代器?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
是否可以在 Java 中合并迭代器?我有兩個(gè)迭代器,我想組合/合并它們,以便我可以一次迭代它們的元素(在同一個(gè)循環(huán)中)而不是兩個(gè)步驟.那可能嗎?
Is it possible to merge iterators in Java? I have two iterators and I want to combine/merge them so that I could iterate though their elements in one go (in same loop) rather than two steps. Is that possible?
請注意,兩個(gè)列表中的元素?cái)?shù)量可能不同,因此對兩個(gè)列表進(jìn)行一次循環(huán)不是解決方案.
Note that the number of elements in the two lists can be different therefore one loop over both lists is not the solution.
Iterator<User> pUsers = userService.getPrimaryUsersInGroup(group.getId());
Iterator<User> sUsers = userService.getSecondaryUsersInGroup(group.getId());
while(pUsers.hasNext()) {
User user = pUsers.next();
.....
}
while(sUsers.hasNext()) {
User user = sUsers.next();
.....
}
推薦答案
Guava(原 Google Collections)有 Iterators.concat.
Guava (formerly Google Collections) has Iterators.concat.
這篇關(guān)于是否可以在 Java 中合并迭代器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!