Android examples for java.util:Collection
Deep clone a collection.
import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import com.elminster.common.util.Messages.Message; public class Main{ public static void main(String[] argv){ Collection c = java.util.Arrays.asList("asdf","book2s.com"); System.out.println(cloneCollection(c)); }/* ww w . ja va 2s .c o m*/ /** * Deep clone a collection. * * @param map * collection to clone * @return cloned map */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static Collection cloneCollection(Collection c) { Collection cloned = null; if (CollectionUtil.isNotEmpty(c)) { try { cloned = (Collection) c.getClass().newInstance(); Iterator it = c.iterator(); while (it.hasNext()) { Object entry = it.next(); if (entry instanceof Cloneable) { Object clone; // try to call clone() try { clone = ReflectUtil.invoke(entry, "clone", new Object[] {}); // $NONNLS1$ // if successful entry = clone; } catch (IllegalArgumentException e) { // ignore } catch (NoSuchMethodException e) { // ignore } catch (InvocationTargetException e) { // ignore } catch (Exception e) { // ignore } } cloned.add(entry); } } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } return cloned; } /** * Check whether the specified collection is not empty * * @param collection * the specified collection * @return whether the the specified collection is not empty */ public static boolean isNotEmpty(Collection<?> collection) { return !isEmpty(collection); } /** * Check whether the specified map is not empty * * @param map * the specified map * @return whether the the specified map is not empty */ public static boolean isNotEmpty(Map<?, ?> map) { return !isEmpty(map); } /** * Check whether the specified collection is empty * * @param collection * the specified collection * @return whether the the specified collection is empty */ public static boolean isEmpty(Collection<?> collection) { return null == collection || collection.isEmpty(); } /** * Check whether the specified map is empty * * @param map * the specified map * @return whether the the specified map is empty */ public static boolean isEmpty(Map<?, ?> map) { return null == map || map.isEmpty(); } }