集册 Java实例教程 将Iterable转换为List

将Iterable转换为List

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

2329
将Iterable转换为List


//package com.nowjava;

import java.util.ArrayList;/*时代Java公众号 - nowjava.com*/


import java.util.Collection;

import java.util.Collections;


import java.util.LinkedList;

import java.util.List;


public class Main {

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

        return (iterable instanceof Collection) ? new LinkedList<T>(

                (Collection<? extends T>) iterable) : new LinkedList<T>() {

            private static final long serialVersionUID = 3109256773218160485L;

            {

                if (iterable != null) {

                    for (final T t : iterable) {

                        add(t);

                    }/* 来自 N  o w  J a v a . c o m*/

                }

            }

        };

    }


    public static <T> List<T> asList(final T t, 
展开阅读全文