Java List from Array asList(Collection c)

Here you can find the source of asList(Collection c)

Description

as List

License

Open Source License

Declaration

public static <T> List<T> asList(Collection<T> c) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008 The University of York.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * /* w w  w.j  a v a2 s.  co  m*/
 * Contributors:
 *     Dimitrios Kolovos - initial API and implementation
 ******************************************************************************/

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

import java.util.List;

public class Main {
    public static List<?> asList(Object o) {
        if (o instanceof List) {
            return (List<?>) o;
        } else if (o instanceof Collection) {
            List<Object> list = createDefaultList();
            list.addAll((Collection<?>) o);
            return list;
        } else {
            List<Object> list = createDefaultList();
            list.add(o);
            return list;
        }
    }

    public static <T> List<T> asList(Collection<T> c) {
        if (c instanceof List) {
            return (List<T>) c;
        } else {
            ArrayList<T> copy = new ArrayList<T>();
            copy.addAll(c);
            return copy;
        }
    }

    public static <T> List<T> createDefaultList() {
        return new ArrayList<T>();
    }
}

Related

  1. asList(byte[] array)
  2. asList(Class... classesArray)
  3. asList(Collection collection)
  4. asList(Collection collection, Class type)
  5. asList(Collection values)
  6. asList(Collection coll)
  7. asList(Collection collection)
  8. asList(double[] array)
  9. asList(E... elements)