Iterator to List - Java Collection Framework

Java examples for Collection Framework:Iterator

Description

Iterator to List

Demo Code

/*/*from w ww. j  av a 2 s.c  o m*/
  You may freely copy, distribute, modify and use this class as long
  as the original author attribution remains intact.  See message
  below.

  Copyright (C) 2003 Christian Pesch. All Rights Reserved.
 */
//package com.java2s;
import java.util.*;

public class Main {
    public static <T> List<T> toList(Iterator<? extends T> iteration) {
        List<T> elements = new ArrayList<T>(1);
        while (iteration.hasNext()) {
            elements.add(iteration.next());
        }
        return elements;
    }
}

Related Tutorials