Java Program to Shuffle Vector Elements


Shuffle() is collection class method in Java, works in a random manner based on the permutation logic on a specific group of list elements. Tree are two distinct types of method in a shuffle class(), depending on the particular parameters.

  • Java Collections shuffle(list) Method.

  • Java Collections shuffle(list, random) Method. In this method, we can arrange the characters randomly to generate some random values. Then we will apply the suffle method on it.

To perform the vector shuffle, we can use Fisher-Yates shuffle algorithm. In this method, we can learn a linear scan on a vector and swap each and every element with a random one.

Here in this article today, we will learn how to shuffle vector elements by using a Java environment.

Algorithm to write Java program to shuffle vector elements

Here is the possible algorithm of a Java code, how we can shuffle the elements of a vector contained string.

  • Step 1 − Start.

  • Step 2 − Declare shuffle package present in a Java environment.

  • Step 3 − Declare a function to shuffle.

  • Step 4 − If, the operation is to shuffle a random vector then declare it.

  • Step 5 − Declare a public class.

  • Step 6 − Take an input array vector.

  • Step 7 − Mention the length of that array.

  • Step 8 − If the declaration is random, declare it.

  • Step 9 − Go for the next.

  • Step 10 − Use a for loop to run the method.

  • Step 11 − Iterate the value.

  • Step 12 − If needed, decrease the value.

  • Step 13 − Swap and change the positions.

  • Step 14 − Take a helper class

  • Step 15 − Declare the change value as equals to the helper class.

  • Step 16 − Enter the argument string.

  • Step 17 − Enter the int string.

  • Step 18 − Declare the sub array.

  • Step 19 − Ask to print the output.

  • Step 20 − Terminate.

Syntax to write Java program to shuffle vector elements

General Syntax:
public static void shuffle(List<?> list)  
public static void shuffle(List<?> list, Random random)  

Possible Code Syntax:

public class Main {
   public static void main(String[] args) {
      Vector<String> v = new Vector<String>();

      v.add("16");
      v.add("07");
      v.add("10");
      v.add("2001");
      v.add("1997");
      System.out.println(v);
      Collections.shuffle(v);
      System.out.println(v);
   }
}

Double Shuffle:

import java.util.*;  
public class CollectionsShuffleTutorialspoint {  
   public static void main(String[] args) {  
      List<String> list = Arrays.asList("X", "Y", "R", "A");  
      System.out.println("List Before Shuffle Here ----> : "+list);  
      Collections.shuffle(list);  
      System.out.println("List After Shuffle Is Here ----> : "+list);  
   }  
}  

Here we have mentioned the possible syntaxes related to the shuffle method. Where you can see the double shuffle process is also possible for the vector elements. With these possible syntax we have tried to build some Java codes later to shuffle the vector elements present in a particular string.

Approach to build Java program to shuffle vector elements

  • Approach 1 − Java Program to Shuffle Vector Elements

  • Approach 2 − Fisher-Yates shuffle algorithm

Java Program to Shuffle Vector Elements

Here we have included some Java build codes by which we can shuffle some vector elements in a simple and random manner.

Example 1

import java.util.Collections;
import java.util.Vector;
public class VectorShuffleElements {
   public static void main(String[] args) { 
      Vector<Integer> vNumbers = new Vector<Integer>();
      vNumbers.add(16);
      vNumbers.add(07);
      vNumbers.add(2001);
      vNumbers.add(1997);
      vNumbers.add(10);
      Collections.shuffle(vNumbers);
      System.out.println("Vector contains are present in the list:----> " + vNumbers);
   }
}

Output

Vector contains are present in the list:----> [16, 2001, 7, 10, 1997]

Example 2

import java.util.Vector;
import java.util.Collections;
public class Tutorialspoint {
	public static void main(String[] args){
      Vector<String> vec07 = new Vector<String>();
      vec07.add("10");
      vec07.add("16");
      vec07.add("7");
      vec07.add("2001");
      vec07.add("1997");
      System.out.println("Original Vector is here ----> : " + vec07);	
      Collections.shuffle(vec07);
      System.out.println("After shuffling we get the set here ---->: " + vec07);
	}
}

Output

Original Vector is here ----> : [10, 16, 7, 2001, 1997]
After shuffling we get the set here ---->: [1997, 10, 7, 16, 2001]

Example 3

import java.util.*;
import java.util.Vector;
import java.util.Collections;
public class ARBRDD {
	public static void main(String[] args){
      Vector<String> vec = new Vector<String>();
      vec.add("13109");
      vec.add("KOAA-DHAKA Maitree Express");
      vec.add("International Railway Connectivity");
      vec.add("India");
      vec.add("Bangladesh");
      System.out.println("Original Vector is here ----> : " + vec);
      Collections.shuffle(vec, new Random());
      System.out.println("\nShuffled Vector with Random() is here ----> : \n" + vec);
      Collections.shuffle(vec, new Random(3));
      System.out.println("\nShuffled Vector with Random(3) is here ---->: \n" + vec);
      Collections.shuffle(vec, new Random(5));
      System.out.println("\nShuffled Vector with Random(5) is here ----> : \n" + vec);
	}
}

Output

Original Vector is here ----> : [13109, KOAA-DHAKA Maitree Express, International Railway Connectivity, India, Bangladesh]

Shuffled Vector with Random() is here ----> : 
[KOAA-DHAKA Maitree Express, 13109, International Railway Connectivity, India, Bangladesh]

Shuffled Vector with Random(3) is here ---->: 
[India, 13109, KOAA-DHAKA Maitree Express, International Railway Connectivity, Bangladesh]

Shuffled Vector with Random(5) is here ----> : 
[International Railway Connectivity, 13109, Bangladesh, India, KOAA-DHAKA Maitree Express]

Example 4

import java.util.Arrays; 
import java.util.Collections; 
import java.util.List; 
import java.util.Random; 
public class RandomizeList { 
   public static void main(String args[]) { 
      List<Integer> numbers = Arrays.asList(16, 7, 10, 2001, 1997, 10, 2022); System.out.println("Particular List Before Shuffling Present Here ---->: " + numbers);
      Collections.shuffle(numbers); 
      System.out.println("The Particular List after shuffling Done --->: " + numbers); 
      Collections.shuffle(numbers, new Random(System.nanoTime())); 
       System.out.println("Particular List After Shuffling Again Done ---->: " + numbers); 
   }
}

Output

Particular List Before Shuffling Present Here ---->: [16, 7, 10, 2001, 1997, 10, 2022]
The Particular List after shuffling Done --->: [1997, 2001, 10, 2022, 7, 10, 16]
Particular List After Shuffling Again Done ---->: [1997, 2022, 10, 10, 16, 7, 2001]

Example 5

import java.util.*;  
public class Greetingsshufflelist {  
   public static void main(String[] args) {          
      List<String> list = Arrays.asList("Hi!", "Hello!", "Hallo!", "Bonjour!");  
      System.out.println(list);  
      Collections.shuffle(list, new Random(7));  
      System.out.println(list);         
   }  
}

Output

[Hi!, Hello!, Hallo!, Bonjour!]
[Hi!, Hello!, Bonjour!, Hallo!]

Fisher-Yates shuffle algorithm to shuffle vector elements

Fisher Yates shuffle Algorithm is an assumption process running method in Java which runs in O(n) complexity. The function called rand() generates a random number in O(1) time.

Example 6

import java.util.Random;
import java.util.Arrays;
public class ShuffleRand{
	static void randomize( int arr[], int n){
      Random r = new Random();
      for (int i = n-1; i > 0; i--) {
      	int j = r.nextInt(i+1);
      	int temp = arr[i];
      	arr[i] = arr[j];
      	arr[j] = temp;
      }
      System.out.println(Arrays.toString(arr));
	}
	public static void main(String[] args){
      
      int[] arr = {16, 7, 10, 2022, 1997, 2001, 25, 11};
      int n = arr.length;
      randomize (arr, n);
	}
}

Output

[1997, 2022, 2001, 25, 11, 16, 7, 10]

Conclusion

From this article today, we have learned about the shuffle methods with some possible java codes by following the syntaxes and algorithm. Hope this article helped you to understand the modus operandi of the various vector shuffling methods mentioned here.

Updated on: 12-Apr-2023

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements