Copy Iterator to List - Java java.util

Java examples for java.util:Iterable Convert

Description

Copy Iterator to List

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static void main(String[] argv) {
        Iterator iter = java.util.Arrays.asList("asdf", "java2s.com")
                .iterator();//w w w. j  av a 2s .co m
        System.out.println(copyIterator(iter));
    }

    /**
     * Iterator to List
     * @param <T>
     * @param iter
     * @return 
     */
    public static <T> List<T> copyIterator(Iterator<T> iter) {

        List<T> copy = new ArrayList<T>();
        while (iter.hasNext())
            copy.add(iter.next());
        return copy;
    }
}

Related Tutorials