Languages
[Edit]
EN

Java - get random element from list

12 points
Created by:
AnnLen
9180

1. Get random element from list with ThreadLocalRandom

public static Integer randomElement() {
    List<Integer> list = Arrays.asList(11, 22, 33, 44, 55, 66, 77);
    int size = list.size();
    int randIdx = ThreadLocalRandom.current().nextInt(size);
    Integer randomElem = list.get(randIdx);
    return randomElem;
}

Example:

System.out.println(randomElement()); // 77
System.out.println(randomElement()); // 11
System.out.println(randomElement()); // 33
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join