Java tutorial
//package com.java2s; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; public class Main { public static <T> String[] compact(Collection<T> col) { Collection<String> result = new ArrayList(); for (T e : col) { String str = e.toString(); if (!str.trim().isEmpty()) result.add(str); } return result.toArray(new String[result.size()]); } public static <T> String[] trim(Collection<T> col) { String[] result = new String[col.size()]; int index = 0; for (T e : col) { result[index++] = e.toString().trim(); } return result; } /** USE NATIVE c.toArray(a) */ @Deprecated public static <T> T[] toArray(Collection<T> c, T[] a) { return c.toArray(a); } /** Returns a new array even if collection is empty */ public static <T> T[] toArray(Collection<T> c, Class klass) { return toArray(c, (T[]) Array.newInstance(klass, c.size())); } /** Returns a new array or null if collection is empty */ public static <T> T[] toArray(Collection<T> c) { return c.isEmpty() ? null : toArray(c, c.iterator().next().getClass()); } }