Java Utililty Methods Collection Random Element

List of utility methods to do Collection Random Element

Description

The list of methods to do Collection Random Element are organized into topic(s).

Method

TchooseElement(Collection elements, T alternative)
choose Element
return chooseElement((T[]) (elements.toArray()), alternative);
ObjectchooseRandomThing(Collection possibleroutes)
Function chooseRandomPath -------------------------- This function takes in a set of possible routes and simply chooses a random element from the list.
if (possibleroutes == null) {
    return null;
int size = possibleroutes.size();
int item = new Random().nextInt(size);
int i = 0;
for (Object p : possibleroutes) {
    if (i == item)
...
TchooseWithoutCheck(Collection collection, long seed)
choose Without Check
int index = new Random(seed).nextInt(collection.size());
Optional<? extends T> result = collection.stream().skip(index).findFirst();
assert result.isPresent();
return result.get();
List>generateMetricsDataWithAllWrongTypes(String metricPostfix, boolean generateRandomTenant, long collectionTime)
generate Metrics Data With All Wrong Types
List<Map<String, Object>> metricsList = new ArrayList<Map<String, Object>>();
Map<String, Object> testMetric = new TreeMap<String, Object>();
if (generateRandomTenant) {
    testMetric.put("tenantId", random.nextInt());
testMetric.put("metricName", "mzord.string.metric" + metricPostfix);
testMetric.put("ttlInSeconds", 1234566);
testMetric.put("unit", "milliseconds");
...
List>generateMetricsDataWithPartialWrongTypes(String metricPostfix, boolean generateRandomTenant, long collectionTime)
generate Metrics Data With Partial Wrong Types
List<Map<String, Object>> metricsList = generateMetricsDataWithAllWrongTypes(metricPostfix,
        generateRandomTenant, collectionTime);
Map<String, Object> testMetric = new TreeMap<String, Object>();
if (generateRandomTenant) {
    testMetric.put("tenantId", random.nextInt());
testMetric.put("metricName", "mzord.small.numeric.metric" + metricPostfix);
testMetric.put("ttlInSeconds", 1234566);
...
VgetRandom(Collection collection)
get Random
if (collection.isEmpty()) {
    return null;
int randIdx = (int) Math.round(Math.random() * (collection.size() - 1));
int count = 0;
Iterator<V> iterator = collection.iterator();
while (iterator.hasNext()) {
    V current = iterator.next();
...
SetgetRandomNumber(Collection collection, int number)
Returns at random a specific number of elements from a collection.
List<T> list = new ArrayList<T>(collection);
Collections.shuffle(list);
return new HashSet<T>(list.subList(0, number));
TnextItem(Collection items, Random random)
next Item
int randomIndex = random.nextInt(items.size());
int count = 0;
for (T item : items) {
    if (count == randomIndex)
        return item;
    count++;
return null;
...
ToneOf(Collection array)
one Of
List<T> list = new ArrayList<T>(array);
return list.get(cInt(0, array.size() - 1));
KpeekRandom(Collection collection, Random rnd)
peek Random
int size = collection.size();
if (size == 0) {
    return null;
int index = rnd.nextInt(size);
List<K> values = new ArrayList<K>(collection);
return values.get(index);