Java List Random Item listChooseOne(List list)

Here you can find the source of listChooseOne(List list)

Description

list Choose One

License

Open Source License

Declaration

public static <T> T listChooseOne(List<T> list) 

Method Source Code

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

import java.util.List;
import java.util.Random;

public class Main {
    private static Random R = new Random();

    public static <T> T listChooseOne(List<T> list) {
        if (list.size() == 1) {
            return list.get(0);
        }/*w  w  w. ja  v a2  s .  c o  m*/
        if (list.size() > 1) {
            return list.get(R.nextInt(list.size()));
        }
        return null;
    }
}

Related

  1. getRandomElement(List myList)
  2. getRandomElementFromList(List l)
  3. getRandomFromList(List list)
  4. getRandomIdxList(int minIdx, int maxIdx)
  5. getRandomIntBetween(int min, int max, List excludeList)
  6. makeListOfIntegersInRange(Integer size, Integer maxOfRange)
  7. makeRandomList(int arraySize, boolean unique)
  8. newRandom(int size, int price, List oldRandomList)
  9. newRandomIntegerList(int size)