从Java列表中随机选择项目
在本文中,我们将展示最有效的方法,即从 List 中查找或选择元素。从列表中选择一个项目的基本思想是,首先生成一个应该在 0 到列表大小之间的数字。
1.单个随机项目
首先,我们使用 Random.nextInt(int bound) 方法选择一个随机索引。代替 Random 类,您始终可以使用静态方法 Math.random()(random() 方法生成一个介于 0 和 1 之间的数字)并将其与列表大小相乘。
Java
// Java program select a random element from array
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GFG {
// Drive Function
public static void main(String[] args)
{
// create a list of Integer type
List list = new ArrayList<>();
// add 5 element in ArrayList
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
GFG obj = new GFG();
// take a random element from list and print them
System.out.println(obj.getRandomElement(list));
}
// Function select an element base on index
// and return an element
public int getRandomElement(List list)
{
Random rand = new Random();
return list.get(rand.nextInt(list.size()));
}
}
Java
// Java program select a random element from List
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class GFG {
// Drive Function
public static void main(String[] args)
{
// create a list of Integer type
List list = new ArrayList<>();
// add 5 element in ArrayList
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
GFG obj = new GFG();
// boundIndex to select in sub list
int boundIndex = 3;
// take a random element from list and print it
System.out.println(
obj.getRandomElement(list, boundIndex));
}
// Function to select an element based on index and
// return an element
public int getRandomElement(List list,
int bound)
{
// ThreadLocalRandom generates an int type number
return list.get(
ThreadLocalRandom.current().nextInt(list.size())
% bound);
}
}
Java
// Java program select a random element from List
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GFG {
// Drive Function
public static void main(String[] args)
{
// create a list of Integer type
List list = new ArrayList<>();
// add 5 element in ArrayList
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
GFG obj = new GFG();
// boundIndex for select in sub list
int numberOfElements = 3;
// take a random element from list and print them
System.out.println(
obj.getRandomElement(list, numberOfElements));
}
// Function select an element base on index and return
// an element
public List
getRandomElement(List list, int totalItems)
{
Random rand = new Random();
// create a temporary list for storing
// selected element
List newList = new ArrayList<>();
for (int i = 0; i < totalItems; i++) {
// take a random index between 0 to size
// of given List
int randomIndex = rand.nextInt(list.size());
// add element in temporary list
newList.add(list.get(randomIndex));
}
return newList;
}
}
Java
// Java program select a random element from List
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GFG {
// Drive Function
public static void main(String[] args)
{
// create a list of Integer type
List list = new ArrayList<>();
// add 5 element in ArrayList
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
GFG obj = new GFG();
// boundIndex for select in sub list
int numberOfElements = 3;
// take a random element from list and print them
System.out.println(
obj.getRandomElement(list, numberOfElements));
}
// Function select an element base on index and return
// an element
public List
getRandomElement(List list, int totalItems)
{
Random rand = new Random();
// create a temporary list for storing
// selected element
List newList = new ArrayList<>();
for (int i = 0; i < totalItems; i++) {
// take a random index between 0 to size
// of given List
int randomIndex = rand.nextInt(list.size());
// add element in temporary list
newList.add(list.get(randomIndex));
// Remove selected element from original list
list.remove(randomIndex);
}
return newList;
}
}
输出
30
2.在多线程环境中选择随机索引
当我们使用单个 Random 类实例处理多线程应用程序时,可能会导致为访问该实例的每个进程选择相同的值。因此,我们总是可以使用 ThreadLocalRandom 类为每个线程创建一个新实例。
Java
// Java program select a random element from List
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class GFG {
// Drive Function
public static void main(String[] args)
{
// create a list of Integer type
List list = new ArrayList<>();
// add 5 element in ArrayList
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
GFG obj = new GFG();
// boundIndex to select in sub list
int boundIndex = 3;
// take a random element from list and print it
System.out.println(
obj.getRandomElement(list, boundIndex));
}
// Function to select an element based on index and
// return an element
public int getRandomElement(List list,
int bound)
{
// ThreadLocalRandom generates an int type number
return list.get(
ThreadLocalRandom.current().nextInt(list.size())
% bound);
}
}
输出
10
3.选择具有重复列表元素的随机项目
有时我们想从列表中挑选一些元素。所以首先知道我们要选择多少元素,然后我们一个一个地选择项目并添加一个新列表并返回它。注意:在这种情况下,一个元素可能会被多次选择,因为我们不会删除选定的元素,因此列表大小保持不变。
Java
// Java program select a random element from List
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GFG {
// Drive Function
public static void main(String[] args)
{
// create a list of Integer type
List list = new ArrayList<>();
// add 5 element in ArrayList
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
GFG obj = new GFG();
// boundIndex for select in sub list
int numberOfElements = 3;
// take a random element from list and print them
System.out.println(
obj.getRandomElement(list, numberOfElements));
}
// Function select an element base on index and return
// an element
public List
getRandomElement(List list, int totalItems)
{
Random rand = new Random();
// create a temporary list for storing
// selected element
List newList = new ArrayList<>();
for (int i = 0; i < totalItems; i++) {
// take a random index between 0 to size
// of given List
int randomIndex = rand.nextInt(list.size());
// add element in temporary list
newList.add(list.get(randomIndex));
}
return newList;
}
}
输出
[40, 20, 40]
4.选择没有重复列表元素的随机项目
有时我们想从列表中挑选一些元素。所以首先确定我们要选择多少元素,然后我们一个一个地选择项目并添加一个新列表并返回它。注意:在这种情况下,一个元素只选择一个,因为我们删除了选定的元素,因此 JVM 也会自动减小列表大小。
Java
// Java program select a random element from List
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GFG {
// Drive Function
public static void main(String[] args)
{
// create a list of Integer type
List list = new ArrayList<>();
// add 5 element in ArrayList
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
GFG obj = new GFG();
// boundIndex for select in sub list
int numberOfElements = 3;
// take a random element from list and print them
System.out.println(
obj.getRandomElement(list, numberOfElements));
}
// Function select an element base on index and return
// an element
public List
getRandomElement(List list, int totalItems)
{
Random rand = new Random();
// create a temporary list for storing
// selected element
List newList = new ArrayList<>();
for (int i = 0; i < totalItems; i++) {
// take a random index between 0 to size
// of given List
int randomIndex = rand.nextInt(list.size());
// add element in temporary list
newList.add(list.get(randomIndex));
// Remove selected element from original list
list.remove(randomIndex);
}
return newList;
}
}
输出
[50, 10, 20]