集册 Java实例教程 返回给定{Iterable}的内容的{List}

返回给定{Iterable}的内容的{List}

欢马劈雪     最近更新时间:2020-01-02 10:19:05

38
返回给定{Iterable}的内容的{List}
//时代Java - N o w  J a v a . c o m 提供

//package com.nowjava;

import java.util.ArrayList;

import java.util.List;


public class Main {

    /**

     * Returns a {List} of the contents for the given {Iterable}

     *

     * @param iterable The Iterable

     * @param <T>      The type of objects contained in the {Iterable}

     * @return A {List} of the iterable's objects

     */

    public static <T> List<T> asList(Iterable<T> iterable) {

        List<T> list = new ArrayList<T>();


        for (T item : iterable) {

            list.add(item);

        }


        return list;

    }

}