Open In App

Java Program to Shuffle Vector Elements

Last Updated : 11 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. The random parameter to specify randomness.
  2. 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]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads