iterator To List - Java java.util

Java examples for java.util:Iterator

Description

iterator To List

Demo Code


//package com.java2s;
import java.util.*;

public class Main {
    public static void main(String[] argv) {
        Iterator iterator = java.util.Arrays.asList("asdf", "java2s.com")
                .iterator();/*from  w w w  .  jav a  2  s .c om*/
        System.out.println(iteratorToList(iterator));
    }

    public static <T> List<T> iteratorToList(final Iterator<T> iterator) {
        List<T> list = new ArrayList<T>();
        for (T element : new Iterable<T>() {
            public Iterator<T> iterator() {
                return iterator;
            }
        }) {
            list.add(element);
        }
        return list;
    }
}

Related Tutorials