Vectors basically fall in legacy classes but now it is fully compatible with collections. Java has many built-in functions to perform different operations on collections or other data types and one of them is shuffle. To shuffle Vector elements Collections.shuffle() method is used. It shuffle method of the Collections class shuffles the elements of the specified Vector object using the default source of the randomness. It randomly permutes the Vector elements passed in parameters.
Application of shuffle() method
- It is used in cryptographic applications.
- Generating unique transaction numbers for a payment field.
- The software in rockets, satellites, airplanes, cryptography utilizes randomization to get a high probability of good results on an algorithm.
Collections shuffle function can also be called in two ways:
- The random parameter to specify randomness.
- Without parameter.
The shuffle method uses the default randomness source to select random elements from the Vector. This function here doesn’t take much time and runs in linear time and each time executed the result can be different.
Class hierarchy:
java
↳ util
↳ Collections
Syntax:
Collections.shuffle(vector).
Parameters: The Vector which you will pass will be shuffled.
Returns: Shuffle function shuffles the Vector element.
Example:
Java
import java.util.Vector;
import java.util.Collections;
public class GFG {
public static void main(String[] args)
{
Vector<String> vec = new Vector<String>();
vec.add( "5" );
vec.add( "6" );
vec.add( "7" );
vec.add( "8" );
vec.add( "9" );
System.out.println( "Original Vector : " + vec);
Collections.shuffle(vec);
System.out.println( "After shuffling : " + vec);
}
}
|
Output:
Original Vector : [5, 6, 7, 8, 9]
After shuffling, Vector : [8, 9, 5, 6, 7]
Shuffling a Vector using Random Function which will become the source of Randomness.
Syntax:
Collections.shuffle(Vector, Random random)
Example:
Java
import java.util.*;
import java.util.Vector;
import java.util.Collections;
public class GFG {
public static void main(String[] args)
{
Vector<String> vec = new Vector<String>();
vec.add( "geeksforgeeks" );
vec.add( "course" );
vec.add( "practice" );
vec.add( "archive" );
vec.add( "interview" );
System.out.println( "Original Vector : " + vec);
Collections.shuffle(vec, new Random());
System.out.println(
"\nShuffled Vector with Random() : \n" + vec);
Collections.shuffle(vec, new Random( 3 ));
System.out.println(
"\nShuffled Vector with Random(3) : \n" + vec);
Collections.shuffle(vec, new Random( 5 ));
System.out.println(
"\nShuffled Vector with Random(5) : \n" + vec);
}
}
|
Output:
Original Vector : [geeksforgeeks, course, practice, archive, interview]
Shuffled Vector with Random() :
[interview, practice, geeksforgeeks, archive, course]
Shuffled Vector with Random(3) :
[archive, practice, interview, geeksforgeeks, course]
Shuffled Vector with Random(5) :
[geeksforgeeks, practice, course, archive, interview]
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 :
11 Dec, 2020
Like Article
Save Article