Here you can find the source of getRandomElement(List
public static <T> T getRandomElement(List<T> elements)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.concurrent.ThreadLocalRandom; public class Main { public static <T> T getRandomElement(List<T> elements) { int size = elements.size(); if (size > 0) { return elements.get(ThreadLocalRandom.current().nextInt(size)); } else {/*from w w w .ja va 2s .c om*/ return null; } } public static <T> T getRandomElement(Collection<T> elements) { List<T> list = new ArrayList<T>(elements); return getRandomElement(list); } }