Java Iterable to List iterableToList(Iterable iterable)

Here you can find the source of iterableToList(Iterable iterable)

Description

Converts an iterable into a list.

License

Apache License

Parameter

Parameter Description
iterable The iterable to be converted.

Return

The list representation of the given iterable, possibly the same instance as that iterable.

Declaration

public static <T> List<T> iterableToList(Iterable<T> iterable) 

Method Source Code

//package com.java2s;
/*//  w w w .  jav  a2  s. c om
 * Copyright 2013 OmniFaces.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

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

import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * Converts an iterable into a list.
     * <p>
     * This method makes NO guarantee to whether changes to the source iterable are
     * reflected in the returned list or not. For instance if the given iterable
     * already is a list, it's returned directly.
     *
     * @param iterable The iterable to be converted.
     * @return The list representation of the given iterable, possibly the same instance as that iterable.
     * @since 1.5
     */
    public static <T> List<T> iterableToList(Iterable<T> iterable) {

        List<T> list = null;

        if (iterable instanceof List) {
            list = (List<T>) iterable;
        } else if (iterable instanceof Collection) {
            list = new ArrayList<T>((Collection<T>) iterable);
        } else {
            list = new ArrayList<T>();
            Iterator<T> iterator = iterable.iterator();
            while (iterator.hasNext()) {
                list.add(iterator.next());
            }
        }

        return list;
    }
}

Related

  1. iterableAsList(Iterable iterable)
  2. iterableToCollection(Iterable c, Collection list)
  3. iterableToCollection(Iterable c, Collection list)
  4. iterableToList(Iterable iterable)
  5. iterableToList(Iterable input)
  6. iterableToSSV(Iterable list, String sep)
  7. toList(Iterable i)
  8. toList(Iterable items)
  9. toList(Iterable iter)