Here you can find the source of iterableToList(Iterable
Parameter | Description |
---|---|
iterable | The iterable to be converted. |
public static <T> List<T> iterableToList(Iterable<T> iterable)
//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; } }