Java Program to Shuffle Vector Elements
Last Updated :
11 Dec, 2020
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
// Java Program to shuffle Vector Elements
// Importing libraries
import java.util.Vector;
import java.util.Collections;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Create a Vector object
Vector<String> vec = new Vector<String>();
// Add elements to Vector(Random)
vec.add("5");
vec.add("6");
vec.add("7");
vec.add("8");
vec.add("9");
// Prints vector element before Shuffling
System.out.println("Original Vector : " + vec);
// The shuffle method of the Collections class
Collections.shuffle(vec);
// Prints vector element after Shuffling and
// each time executed the result can be different
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
// Java Program to shuffle Vector Elements
// Importing java libraries
import java.util.*;
import java.util.Vector;
import java.util.Collections;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Create a Vector object
Vector<String> vec = new Vector<String>();
// Add elements to Vector (Random)
vec.add("geeksforgeeks");
vec.add("course");
vec.add("practice");
vec.add("archive");
vec.add("interview");
// Prints vector element before Shuffling
System.out.println("Original Vector : " + vec);
// The Random Function
Collections.shuffle(vec, new Random());
System.out.println(
"\nShuffled Vector with Random() : \n" + vec);
// Random(3) to shuffle given vector
Collections.shuffle(vec, new Random(3));
System.out.println(
"\nShuffled Vector with Random(3) : \n" + vec);
// Random(3) to shuffle given list
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]
Similar Reads
Java Program to Copy Elements of ArrayList to Vector Vector implements List Interface, like ArrayList it also maintains insertion order but it is rarely used in the non-thread environment as it is synchronized, and due to which it gives a poor performance in adding, searching, deleting, and updating of its elements. To copy elements from one collectio
4 min read
Java Program to Rotate Matrix Elements A matrix is simply a two-dimensional array. So, the goal is to deal with fixed indices at which elements are present and to perform operations on indexes such that elements on the addressed should be swapped in such a manner that it should look out as the matrix is rotated.For a given matrix, the ta
6 min read
How to Shuffle the Elements of Array in Java? In Java, to shuffle the elements of an array, we can use the shuffle() method from the Collections class. This method is part of the java.util package. To shuffle an array, first, we need to convert the array into a List, shuffle it, and then convert it back to an array if needed. Example:Let's go t
3 min read
Copy Elements of Vector to Java ArrayList Since Vector class and ArrayList class both are part of Java Collections, ie Collection framework, so both of these classes can use methods available to the Collection framework. Copy() method is one of the methods of Collection Interface which is used to copy one list to another list, here list can
3 min read
Java Program to Iterate Vector using Enumeration The Vector class implements a growable array of objects. It is available in java.util package. It implements the List interface. The Enumeration interface defines the methods by which you can traverse the elements in a collection of objects. Now in order to add elements Vector Syntax: public class V
2 min read