How to Get Random Elements From the Vector in Java?
Last Updated :
07 Jan, 2021
Vector in java is a part of Java’s collections framework. Vector is a dynamic array of objects, i.e., the size of the vector can be modified as per the requirement. Vector implements the List interface. It also maintains the insertion order and the elements of the vector can be accessed using their indexes. The vector in java is synchronized.
We can access the vector elements in the following ways:
- Using their indexes
- By using the iterator
- By randomly calling the elements from the vector
Different ways to get random elements from the vector:
- Using random() method of Math class
- Using Random class
- Using ThreadLocalRandom class
Method 1: Using random() method of Math class
The Math class of the java.lang package has a method random() which returns a positive double value which is greater than 0.0 and less than 1.0. We can use this method to generate a random index and access the element present at that index in the given vector.
Java
import java.util.Vector;
public class GfG {
static Vector<String> vector;
static void getRandomElements()
{
for ( int i = 0 ; i < vector.size(); i++)
{
int index = ( int )(Math.random() * vector.size());
System.out.println(vector.get(index));
}
}
public static void main(String[] args)
{
vector = new Vector<String>();
vector.add( "Welcome" );
vector.add( "To" );
vector.add( "Geeks" );
vector.add( "For" );
vector.add( "Geeks" );
getRandomElements();
}
}
|
Output
For
To
Welcome
Welcome
Geeks
Method 2: Using Random class
To generate the random index we can also use the Random class of the java.util package. It provides useful methods to generate random numbers of the specified type and within specified ranges.
Java
import java.util.Random;
import java.util.Vector;
class GFG {
static Vector<String> vector;
static void getRandomElements()
{
Random random = new Random();
for ( int i = 0 ; i < vector.size(); i++)
{
int index = random.nextInt(vector.size());
System.out.println(vector.get(index));
}
}
public static void main(String[] args)
{
vector = new Vector<String>();
vector.add( "Welcome" );
vector.add( "To" );
vector.add( "Geeks" );
vector.add( "For" );
vector.add( "Geeks" );
getRandomElements();
}
}
|
Output
Welcome
Geeks
To
Geeks
For
Method 3: Using ThreadLocalRandom class
ThreadLocalRandom class of the java.util.concurrent is a random number generator isolated to the current thread. It generates a random number of a specified type and within the specified range for each thread in a multi-threading environment. We can generate the random index using the nextInt() method of this class and access the element at that index from the given vector.
Java
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
class GFG {
static Vector<String> vector;
static void getRandomElements()
{
for ( int i = 0 ; i < vector.size(); i++)
{
int index = ThreadLocalRandom.current().nextInt(vector.size());
System.out.println(vector.get(index));
}
}
public static void main(String[] args)
{
vector = new Vector<String>();
vector.add( "Welcome" );
vector.add( "To" );
vector.add( "Geeks" );
vector.add( "For" );
vector.add( "Geeks" );
getRandomElements();
}
}
|
Output
Geeks
Welcome
Geeks
To
To
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...