Java Iterator to List toList(Iterator src)

Here you can find the source of toList(Iterator src)

  1. HOME
  2. Java
  3. I
  4. Iterator to List
  5. toList(Iterator src)

Description

Creates a List containing all the elements from an Iterator.

License

Open Source License

Declaration

public static <A> List<A> toList(Iterator<A> src) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Iterator;

public class Main {
    /**//from ww w .  j  a  v a 2 s.c o  m
     * Creates a List containing all the elements from an Iterator.
     */
    public static <A> List<A> toList(Iterator<A> src) {
        LinkedList<A> l = new LinkedList<A>();
        addAll(l, src);
        return l;
    }

    /**
     * Adds all the elements from an iterator to a collection.
     */
    public static <A> void addAll(Collection<A> dest,
            Iterator<? extends A> src) {
        while (src.hasNext())
            dest.add(src.next());
    }
}

Related

  1. toList(Iterator iterator)
  2. toList(Iterator iterator)
  3. toList(Iterator iterator)
  4. toList(Iterator it)
  5. toList(Iterator iter)

HOME | Copyright © www.java2s.com 2016