public final class SetUtilities extends Object
Constructor and Description 

SetUtilities() 
Modifier and Type  Method and Description 

static <T> T 
getRandomElement(Set<? extends T> set,
Random rg)
Select a random element from a given set (uniformly distributed).

static <T> Set<T> 
getRandomSubset(Set<? extends T> set,
int size,
Random rg)
Selects a random subset of a specific size from a given set (uniformly distributed).

static <T> Collection<T> 
getRandomSubset(Set<? extends T> set,
int size,
Random rg,
Collection<T> subset)
Selects a random subset of a specific size from a given set (uniformly distributed).

public static <T> T getRandomElement(Set<? extends T> set, Random rg)
Iterator.next()
is returned. In the worst case, this algorithm has
linear time complexity with respect to the size of the given set.T
 type of randomly selected elementset
 set from which to select a random elementrg
 random generatorpublic static <T> Collection<T> getRandomSubset(Set<? extends T> set, int size, Random rg, Collection<T> subset)
Selects a random subset of a specific size from a given set (uniformly distributed). Applies a full scan algorithm that iterates once through the given set and selects each item with probability (#remaining to select)/(#remaining to scan). It can be proven that this algorithm creates uniformly distributed random subsets (for example, a proof is given here). In the worst case, this algorithm has linear time complexity with respect to the size of the given set.
The items from the selected subset are added to the given collection subset
. This collection is
not cleared so that already contained items will be retained. A reference to this same collection is
returned after it has been modified. To store the generated subset in a newly allocated LinkedHashSet
the alternative method getRandomSubset(Set, int, Random)
may also be used.
T
 type of elements in randomly selected subsetset
 set from which a random subset is to be sampledsize
 desired subset size, should be a number in [0,set]rg
 random generatorsubset
 collection to which the items from the selected subset are addedsubset
after it has been filled with the
items from the randomly generated subsetIllegalArgumentException
 if an invalid subset size outside [0,set] is specifiedpublic static <T> Set<T> getRandomSubset(Set<? extends T> set, int size, Random rg)
Selects a random subset of a specific size from a given set (uniformly distributed). Applies a full scan algorithm that iterates once through the given set and selects each item with probability (#remaining to select)/(#remaining to scan). It can be proven that this algorithm creates uniformly distributed random subsets (for example, a proof is given here). In the worst case, this algorithm has linear time complexity with respect to the size of the given set.
The generated subset is stored in a newly allocated LinkedHashSet
.
T
 type of elements in randomly selected subsetset
 set from which a random subset is to be selectedsize
 desired subset size, should be a number in [0,set]rg
 random generatorLinkedHashSet
.IllegalArgumentException
 if an invalid subset size outside [0,set] is specifiedCopyright © 2016. All rights reserved.