public static <T> T getRandomElement(final Set<T> collection) { if (collection == null || collection.size() == 0) { return null; } final int elemIndex = MathUtil.randomInteger(collection.size()); int i = -1; for (final T elem : collection) { if (++i == elemIndex) { return elem; } } return collection.iterator().next(); }
public static <T> T getRandomElement(final T[] collection) { if (collection == null || collection.length == 0) { return null; } return collection[MathUtil.randomInteger(collection.length)]; }
public static <T> T getRandomElement(final List<T> collection) { if (collection == null || collection.size() == 0) { return null; } return collection.get(MathUtil.randomInteger(collection.size())); }