In this article, we will show the most efficient way finding or picking a element from List.
The basic idea for pick an item from list is, First generate a number which should be between 0 to list size.
1. Single Random Item
First we select a random index for using Random.nextInt(int bound) method. Instead of Random class, you can always use static method Math.random()(random() method generate an number between 0 to 1) and multiply it with list size.
2. Select Random Index In Multithread Environment
When we work with multithread applications using the single Random class instance then might result in picking same value for every process accessing this instance. Hence, We can always create a new instance per thread by using ThreadLocalRandom class.
3. Select Random Items With Repetitions List Element
Sometimes we want to pick few elements from a list. so first know how much element we want to select after that we select items one by one and add a new list and return it. Note: in this case one element may be select many times because we are not remove selected elements so list size remaining same.
[10, 50, 20]
4. Select Random Items Without Repetitions List Element
Sometimes we want to pick few elements from a list. so first make sure how much element we want to select after that we select items one by one and add a new list and return it. Note: in this case one element select only ones because we are remove selected elements, so decrease list size also automatic by JVM.
[50, 40, 30]
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- How to randomly select rows from Pandas DataFrame
- Swapping items of a list in Java : Collections.swap() with Example
- How to add all items from a collection to an ArrayList in Java?
- How to add selected items from a collection to an ArrayList in Java?
- Performing Database Operations in Java | SQL CREATE, INSERT, UPDATE, DELETE and SELECT
- Find the top K items with the highest value
- Find K items with the lowest values
- Difference between dict.items() and dict.iteritems() in Python
- select command in Linux with examples
- CSS | user-select Property
- How to get multiple selected values of select box in php?
- How to make a placeholder for a 'select' box?
- How to select multiple columns in a pandas dataframe
- Select row with maximum and minimum value in Pandas dataframe
- How to select all elements without a given class using jQuery ?
- Why does AngularJS include an empty option in select ?
- How to make checkbox visible when hover or select the element?
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer in Java
- How to iterate over a 2D list (list of lists) in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.