Java examples for java.util:Collection Convert
Converts a collection to a list by either casting or creating a concrete list depending on the type of collection given.
//package com.book2s; import java.util.ArrayList; import java.util.Collection; import java.util.List; public class Main { public static void main(String[] argv) { Collection c = java.util.Arrays.asList("asdf", "book2s.com"); System.out.println(listFromCollection(c)); }// www . java 2s . c o m /** * Converts a collection to a list by either casting or creating a concrete * list depending on the type of collection given. * @param <T> The type of the elements in the collection. * @param c the collection. If <code>null</code>, <code>null</code> is * returned. * @return {@link List} containing the same elements as the given * {@link Collection} param. */ public static <T> List<T> listFromCollection(Collection<T> c) { if (c == null) return null; if (c.size() < 1) { return new ArrayList<T>(0); } return (c instanceof List<?>) ? (List<T>) c : new ArrayList<T>(c); } }