Java tutorial
//package com.java2s; import java.util.*; public class Main { public static ArrayList asArrayList(Collection c) { return (ArrayList) asTargetTypeCollection(c, ArrayList.class); } public static Collection asTargetTypeCollection(Collection c, Class targetCollectionClass) { if (targetCollectionClass == null) throw new IllegalArgumentException("'targetCollectionClass' must be not null"); if (c == null) return null; if (targetCollectionClass.isInstance(c)) return c; Collection result = null; try { result = (Collection) targetCollectionClass.newInstance(); } catch (Exception e) { throw new IllegalArgumentException( "targetCollectionClass=" + targetCollectionClass.getName() + " is not correct!", e); } result.addAll(c); return result; } }