大漠駝鈴

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

          JAVA Iterator 轉成 List

          List轉到Iterator容易,JDK本身就支持,反過來的實現方式如下:
          1.使用Apache Common Collections
          2.自己實現的方法轉換
          3.Guaa實現轉換



          方式1:
          #Apache Commons Collections:
          import org.apache.commons.collections.IteratorUtils;
          Iterator<Element> myIterator = //some iterator
          List<Element> myList=IteratorUtils.toList(myIterator);   


          方式二:
          或者自己轉換
          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基礎知識

          評論

          # re: JAVA Iterator 轉成 List  回復  更多評論   

          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   回復  更多評論   

          thanks for share, i like very much, i will come back again ...............................................................................................................................................................................................................................................................................................................LIKE
          2014-12-11 10:58 | flivgames
          主站蜘蛛池模板: 彰武县| 县级市| 松桃| 石首市| 辉南县| 北安市| 潼南县| 即墨市| 醴陵市| 长子县| 永兴县| 华亭县| 故城县| 五河县| 肇源县| 随州市| 永城市| 息烽县| 冕宁县| 荣成市| 旌德县| 林西县| 丰原市| 龙州县| 通江县| 泰兴市| 棋牌| 仁怀市| 左权县| 根河市| 广宁县| 铜山县| 台北市| 应城市| 屏东县| 房山区| 靖安县| 聂荣县| 寻甸| 安达市| 兴山县|