In this article, we will show the most efficient way, finding or picking an element from the List. The basic idea for pick an item from the list is, First generate a number that 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 the static method Math.random()(random() method generate a number between 0 and 1) and multiply it with list size.
Java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GFG {
public static void main(String[] args)
{
List<Integer> list = new ArrayList<>();
list.add( 10 );
list.add( 20 );
list.add( 30 );
list.add( 40 );
list.add( 50 );
GFG obj = new GFG();
System.out.println(obj.getRandomElement(list));
}
public int getRandomElement(List<Integer> list)
{
Random rand = new Random();
return list.get(rand.nextInt(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 the same value for every process accessing this instance. Hence, We can always create a new instance per thread by using ThreadLocalRandom class.
Java
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class GFG {
public static void main(String[] args)
{
List<Integer> list = new ArrayList<>();
list.add( 10 );
list.add( 20 );
list.add( 30 );
list.add( 40 );
list.add( 50 );
GFG obj = new GFG();
int boundIndex = 3 ;
System.out.println(
obj.getRandomElement(list, boundIndex));
}
public int getRandomElement(List<Integer> list,
int bound)
{
return list.get(
ThreadLocalRandom.current().nextInt(list.size())
% bound);
}
}
|
3. Select Random Items With Repetitions List Element
Sometimes we want to pick a 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 the list size remaining the same.
Java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GFG {
public static void main(String[] args)
{
List<Integer> list = new ArrayList<>();
list.add( 10 );
list.add( 20 );
list.add( 30 );
list.add( 40 );
list.add( 50 );
GFG obj = new GFG();
int numberOfElements = 3 ;
System.out.println(
obj.getRandomElement(list, numberOfElements));
}
public List<Integer>
getRandomElement(List<Integer> list, int totalItems)
{
Random rand = new Random();
List<Integer> newList = new ArrayList<>();
for ( int i = 0 ; i < totalItems; i++) {
int randomIndex = rand.nextInt(list.size());
newList.add(list.get(randomIndex));
}
return newList;
}
}
|
4. Select Random Items Without Repetitions List Element
Sometimes we want to pick a 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 selects only ones because we are remove selected elements, so decrease list size also automatic by JVM.
Java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GFG {
public static void main(String[] args)
{
List<Integer> list = new ArrayList<>();
list.add( 10 );
list.add( 20 );
list.add( 30 );
list.add( 40 );
list.add( 50 );
GFG obj = new GFG();
int numberOfElements = 3 ;
System.out.println(
obj.getRandomElement(list, numberOfElements));
}
public List<Integer>
getRandomElement(List<Integer> list, int totalItems)
{
Random rand = new Random();
List<Integer> newList = new ArrayList<>();
for ( int i = 0 ; i < totalItems; i++) {
int randomIndex = rand.nextInt(list.size());
newList.add(list.get(randomIndex));
list.remove(randomIndex);
}
return newList;
}
}
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Jun, 2021
Like Article
Save Article