JAVA Iterator 轉(zhuǎn)成 List 的操作
List轉(zhuǎn)到Iterator容易,JDK本身就支持,反過來的實(shí)現(xiàn)方式如下:
1.使用Apache Common Collections
2.自己實(shí)現(xiàn)的方法轉(zhuǎn)換
3.Guaa實(shí)現(xiàn)轉(zhuǎn)換
方式1:
#Apache Commons Collections: import org.apache.commons.collections.IteratorUtils; Iterator<Element> myIterator = //some iterator List<Element> myList=IteratorUtils.toList(myIterator);
方式2:
或者自己轉(zhuǎn)換
public static <T> List<T> copyIterator(Iterator<T> iter) {
List<T> copy = new ArrayList<T>();
while (iter.hasNext())
copy.add(iter.next());
return copy;
}
使用方式:
List<String> list = Arrays.asList("1", "2", "3");
Iterator<String> iter = list.iterator();
List<String> copy = copyIterator(iter);
方式3:
#Guava import com.google.common.collect.Lists; Iterator<Element> myIterator = //some iterator List<Element> myList = Lists.newArrayList(myIterator);
補(bǔ)充:數(shù)組與List、Arraylist互相轉(zhuǎn)換,迭代器Iterator的一些用法
一、數(shù)組轉(zhuǎn)換為List或ArrayList
1.String類型數(shù)組轉(zhuǎn)換為ArrayList
public void test(){
String[] str = {"first","second","third"};
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(str));
}
asList(T):返回由指定數(shù)組支持的固定大小的列表。
2.int類型數(shù)組轉(zhuǎn)換為List
public void test(){
int[] nums = {5,6,8,4,7,3};
List<Integer> list = Arrays.stream(nums).boxed().collect(Collectors.toList());
}
Arrays.stream將 int[] 轉(zhuǎn)換成 IntStream.
使用IntStream.boxed()進(jìn)行裝箱,將IntStream轉(zhuǎn)換成Stream.
Stream.collect()將Stream轉(zhuǎn)換成List,得到List.
二、List轉(zhuǎn)換為數(shù)組
toArray()方法
public void test(){
List<String> list = new ArrayList<String>();
list.add("first");
list.add("second");
String[] str1 = list.toArray(new String[list.size()]);
}
三、Iterator遍歷List
public void test(){
List<String> list = new ArrayList<String>();
list.add("first");
list.add("second");
for(Iterator<String> i = list.iterator(); i.hasNext();){
String str = i.next();
System.out.println(str);
}
}
hasNext():如果迭代具有更多的元素,則返回true 。用于判定List是否還有元素,有則返回true,繼續(xù)迭代。
next():返回迭代中的下一個(gè)元素。用來獲取元素。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Spring如何動(dòng)態(tài)自定義logback日志目錄詳解
這篇文章主要給大家介紹了關(guān)于Spring如何動(dòng)態(tài)自定義logback日志目錄的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
java開發(fā)中為什么雙重效驗(yàn)鎖要加volatile
這篇文章主要為大家介紹了java開發(fā)中為什么雙重效驗(yàn)鎖要加volatile原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
SpringBoot實(shí)現(xiàn)郵件推送的詳細(xì)代碼
在項(xiàng)目中經(jīng)常會(huì)遇到SpringBoot推送消息的業(yè)務(wù),除了站內(nèi)推送通知,郵件推送也是一種常見的方式,本文小編就帶大家實(shí)現(xiàn)郵件推送,文中有詳細(xì)代碼講解,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04
使用bat啟動(dòng)springboot項(xiàng)目并解決亂碼問題
這篇文章主要介紹了window中使用bat啟動(dòng)springboot項(xiàng)目,并解決亂碼問題2021-06-06
SpringBoot啟動(dòng)并初始化執(zhí)行sql腳本問題
這篇文章主要介紹了SpringBoot啟動(dòng)并初始化執(zhí)行sql腳本問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Java編程使用Runtime和Process類運(yùn)行外部程序的方法
這篇文章主要介紹了Java編程使用Runtime和Process類運(yùn)行外部程序的方法,結(jié)合實(shí)例形式分析了java使用Runtime.getRuntime().exec()方法運(yùn)行外部程序的常見情況與操作技巧,需要的朋友可以參考下2017-08-08

