Java Collection Create toCollection(final Iterable iterable)

Here you can find the source of toCollection(final Iterable iterable)

Description

Converts an Iterable into a Collection.

License

Open Source License

Parameter

Parameter Description
iterable the iterable to convert

Return

a Collection containing all elements of the iterable

Declaration

public static <E> Collection<E> toCollection(final Iterable<E> iterable) 

Method Source Code


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

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**//from  w w  w  .  j  av a2 s . co m
     * Converts an <code>Iterable</code> into a <code>Collection</code>. This
     * method returns an <code>ArrayList</code> containing all elements returned
     * by the iterator generated by <code>iterable</code> in the order they are
     * returned.
     *
     * @param iterable the iterable to convert
     * @return a <code>Collection</code> containing all elements of the iterable
     */
    public static <E> Collection<E> toCollection(final Iterable<E> iterable) {
        final Collection<E> col = new ArrayList<E>();
        final Iterator<E> it = iterable.iterator();
        while (it.hasNext()) {
            final E item = it.next();
            col.add(item);
        }
        return col;
    }
}

Related

  1. stringToCollection(String string, String delim)
  2. toCollection(C c, E... elements)
  3. toCollection(Class cls, Iterable iterable)
  4. toCollection(Class cls, T[] array)
  5. toCollection(final char[] charArray)
  6. toCollection(final T[] elements)
  7. toCollection(Iterable iterable)
  8. toCollection(Iterable i)
  9. toCollection(Object o)