怎么在JAVA項(xiàng)目中將Iterator轉(zhuǎn)換成List-創(chuàng)新互聯(lián)

這篇文章給大家介紹怎么在JAVA項(xiàng)目中將Iterator轉(zhuǎn)換成 List,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

成都創(chuàng)新互聯(lián)公司自2013年起,先為平川等服務(wù)建站,平川等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為平川企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

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)換為L(zhǎng)ist或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)換為L(zhǎng)ist

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è)元素。用來獲取元素。

關(guān)于怎么在JAVA項(xiàng)目中將Iterator轉(zhuǎn)換成 List就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

文章題目:怎么在JAVA項(xiàng)目中將Iterator轉(zhuǎn)換成List-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://muchs.cn/article4/hidie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、企業(yè)網(wǎng)站制作、用戶體驗(yàn)微信小程序、網(wǎng)站收錄、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營