Java Utililty Methods List Random Item

List of utility methods to do List Random Item

Description

The list of methods to do List Random Item are organized into topic(s).

Method

CollectionnewRandomStringList(int size, String... strings)
Create a Collection containing the given size of the given strings.
if (size < 0) {
    throw new IllegalArgumentException("Size cannot be less than 0. ");
if (strings == null || strings.length == 0) {
    throw new IllegalArgumentException("strings must contains at least one String. ");
List<String> list = new ArrayList<String>(size);
for (String string : strings) {
...
StringnewRandomV1(int size, int price, List oldRandomList)
new Random V
long start = System.currentTimeMillis();
List<String> randomList = new ArrayList<String>();
int r = 10000000;
for (int i = 1; i <= price; i++) {
    if (!oldRandomList.contains(String.valueOf(r + i))) {
        randomList.add(String.valueOf(r + i));
long mmmm = System.currentTimeMillis();
long secs = (mmmm - start) / 1000;
System.out.println(secs);
List<String> ranList = new ArrayList<String>();
for (int j = 0; j < size; j++) {
    if (randomList.size() > 0) {
        int ran = new Random().nextInt(randomList.size());
        ranList.add((String) randomList.get(ran));
        randomList.remove(ran);
return ranList.toString().replaceAll("\\s+", "").replace("[", "").replace("]", "");
TnextElement(List list)
Gets a random element from the given list.
if (list != null) {
    int n = list.size();
    if (n > 0x00) {
        return list.get(nextInt(n));
return null;
TnextRandomElement(final List list)
Returns the next random element from the given Collection
if (list.size() == 0) {
    throw new RuntimeException("list.size () must be > 0");
final int lowerBound = 0;
final int upperBound = list.size();
final int index = lowerBound + random.nextInt(upperBound - lowerBound);
return list.get(index);
ListpickNAtRandom(List vals, int n, long seed)
pick N At Random
if (vals.size() <= n) {
    return vals;
HashSet<T> elems = new HashSet<T>();
Random rand = new Random(seed);
for (int i = 0; i < n; i++) {
    boolean added = true;
    do {
...
TpickOneAtRandom(List list)
pick One At Random
return pickOneAtRandom(list, RANDOM);
ObjectPickRandom(final Collection list)
Returns a random object from the list supplied.
return PickRandom(list.toArray());
LinkedListpickRandom(LinkedList list, int n)
pick Random
if (n <= 0)
    throw new IllegalArgumentException("Parameter n must be > 0");
if (n > list.size())
    return list;
int l = list.size();
LinkedList<T> temp = list;
LinkedList<T> res = new LinkedList<T>();
Random r = new Random();
...
TpickRandom(List collection)
pick Random
int index = rnd.nextInt(collection.size());
return collection.get(index);
StringpickupFromList(String[] list)
pickup From List
return list[getRandomNumberBelow(list.length)];