示例#1
0
 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();
 }
示例#2
0
 public static <T> T getRandomElement(final T[] collection) {
   if (collection == null || collection.length == 0) {
     return null;
   }
   return collection[MathUtil.randomInteger(collection.length)];
 }
示例#3
0
 public static <T> T getRandomElement(final List<T> collection) {
   if (collection == null || collection.size() == 0) {
     return null;
   }
   return collection.get(MathUtil.randomInteger(collection.size()));
 }