Java List from Array asList(Collection collection, Class type)

Here you can find the source of asList(Collection collection, Class type)

Description

Convert the collection to a typed list of given type.

License

Open Source License

Parameter

Parameter Description
T a parameter
collection a parameter
type a parameter

Declaration

public static <T> List<T> asList(Collection<?> collection, Class<T> type) 

Method Source Code


//package com.java2s;
/*/*from   w ww . j  ava  2s. c om*/
 *  Copyright (c) Ludger Solbach. All rights reserved.
 *  The use and distribution terms for this software are covered by the
 *  Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
 *  which can be found in the file license.txt at the root of this distribution.
 *  By using this software in any fashion, you are agreeing to be bound by
 *  the terms of this license.
 *  You must not remove this notice, or any other, from this software.
 */

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

import java.util.List;

public class Main {
    /**
     * Convert the collection to a typed list of given type. 
     * @param <T>
     * @param collection
     * @param type
     * @return
     */
    public static <T> List<T> asList(Collection<?> collection, Class<T> type) {
        List<T> list = new ArrayList<T>();
        for (Object o : collection) {
            if (type.isInstance(o)) {
                list.add(type.cast(o));
            }
        }
        return list;
    }
}

Related

  1. asList()
  2. asList(boolean[] array)
  3. asList(byte[] array)
  4. asList(Class... classesArray)
  5. asList(Collection collection)
  6. asList(Collection values)
  7. asList(Collection c)
  8. asList(Collection coll)
  9. asList(Collection collection)