Here you can find the source of getRandomNumber(Collection
Parameter | Description |
---|---|
collection | the collection |
number | the number of elements |
public static <T> Set<T> getRandomNumber(Collection<T> collection, int number)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; public class Main { /**/*from www.ja va2 s . co m*/ * Returns at random a specific number of elements from a collection. * @param collection the collection * @param number the number of elements * @return the random elements */ public static <T> Set<T> getRandomNumber(Collection<T> collection, int number) { List<T> list = new ArrayList<T>(collection); Collections.shuffle(list); return new HashSet<T>(list.subList(0, number)); } }