Java List Random Item choose(Random random, List list)

Here you can find the source of choose(Random random, List list)

Description

choose

License

Open Source License

Declaration

public static <T> T choose(Random random, List<T> list) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

import java.util.Random;

public class Main {
    public static <T> T choose(Random random, List<T> list) {
        if (random == null)
            return list.get(0);
        return list.get(random.nextInt(list.size()));
    }/*from   www. j  ava2  s  .  c o  m*/

    public static <T> T choose(Random random, T... elements) {
        return elements[random.nextInt(elements.length)];
    }

    public static <T> T choose(Random random, Collection<T> set) {
        return choose(random, new ArrayList<T>(set));
    }
}

Related

  1. asRandomAccessList(Collection list)
  2. choice(List list)
  3. choice(T... list)
  4. chooseRandom(List list)
  5. chooseRandomElement(List list, Random random)
  6. computeCrossProduct(List> allArgPossibilities, int maximumSize)
  7. findFirst(List list, T value, Comparator comparator)