大漠駝鈴

          置身浩瀚的沙漠,方向最為重要,希望此blog能向大漠駝鈴一樣,給我方向和指引。
          Java,Php,Shell,Python,服務(wù)器運(yùn)維,大數(shù)據(jù),SEO, 網(wǎng)站開發(fā)、運(yùn)維,云服務(wù)技術(shù)支持,IM服務(wù)供應(yīng)商, FreeSwitch搭建,技術(shù)支持等. 技術(shù)討論QQ群:428622099
          隨筆 - 238, 文章 - 3, 評論 - 117, 引用 - 0
          數(shù)據(jù)加載中……

          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);   


          方式二:
          或者自己轉(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);

          posted on 2014-11-29 18:26 草原上的駱駝 閱讀(21295) 評論(2)  編輯  收藏 所屬分類: JAVA基礎(chǔ)知識

          評論

          # re: JAVA Iterator 轉(zhuǎn)成 List  回復(fù)  更多評論   

          public static <T> List<T> copyIterator(Iterator<T> iter) {
          List<T> copy = new ArrayList<T>();
          while (iter.hasNext())
          copy.add(iter.next());
          return copy;
          }
          // Close
          2014-12-01 16:28 | Kizi 2015

          # thanks   回復(fù)  更多評論   

          thanks for share, i like very much, i will come back again ...............................................................................................................................................................................................................................................................................................................LIKE
          2014-12-11 10:58 | flivgames
          主站蜘蛛池模板: 江华| 南开区| 苍南县| 稻城县| 社旗县| 樟树市| 永德县| 肃宁县| 沅陵县| 涞水县| 虹口区| 临湘市| 嘉黎县| 渭南市| 深水埗区| 准格尔旗| 福清市| 东辽县| 嘉祥县| 隆化县| 勐海县| 墨竹工卡县| 石台县| 大石桥市| 合肥市| 和田县| 高州市| 车险| 沾益县| 神池县| 高雄市| 灌云县| 攀枝花市| 金坛市| 洪雅县| 台中市| 定州市| 巩义市| 弥渡县| 旬阳县| 紫阳县|