Convert Iterable<? extends T> as List - Java java.util

Java examples for java.util:Iterable Convert

Description

Convert Iterable<? extends T> as List

Demo Code


//package com.java2s;
import java.util.ArrayList;

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;
            {/*from   ww  w.  ja  v  a  2s  .  c om*/
                if (iterable != null) {
                    for (final T t : iterable) {
                        add(t);
                    }
                }
            }
        };
    }

    public static <T> List<T> asList(final T t, final T... ts) {
        final ArrayList<T> list = new ArrayList<T>(ts.length + 1);
        list.add(t);
        Collections.addAll(list, ts);
        return list;
    }
}

Related Tutorials