Java Collections shuffle() Method



Description

The Java Collections shuffle(List<?>) method is used to randomly permute the specified list using a default source of randomness.

Declaration

Following is the declaration for java.util.Collections.shuffle() method.

public static void shuffle(List<?> list)

Parameters

list − The list to be shuffled.

Return Value

NA

Exception

UnsupportedOperationException − Throws if the specified list or its list-iterator does not support the set operation.

Java Collections shuffle(List<?>, Random) Method

Description

The Java Collections shuffle(List<?>, Random) method is used to randomly permute the specified list using the specified source of randomness.

Declaration

Following is the declaration for java.util.Collections.shuffle() method.

public static void shuffle(List<?> list,Random rnd)				 

Parameters

  • list − The list to be shuffled.

  • rnd − The source of randomness to use to shuffle the list.

Return Value

NA

Exception

UnsupportedOperationException − This is thrown if the specified list or its list-iterator does not support the set operation.

Getting a Shuffled List from a List of Integer Example

The following example shows the usage of Java Collection shuffle(List) method. We've created a List object with some integers, printed the original list. Using shuffle(List) method, we've shuffled elements of the list and then printed the updated list.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5));

      System.out.println("Initial collection value: " + list);
      // shuffle this collection
      Collections.shuffle(list);
      System.out.println("Final collection value: "+list);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Initial collection value: [1, 2, 3, 4, 5]
Final collection value: [5, 1, 2, 4, 3]

Getting a Shuffled List from a List of String Example

The following example shows the usage of Java Collection shuffle(List) method. We've created a List object with some strings, printed the original list. Using shuffle(List) method, we've shuffled elements of the list and then printed the updated list.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsDemo {

   public static void main(String[] args) {
      List<String> list = new ArrayList<>(Arrays.asList("Welcome","to","Tutorialspoint"));

      System.out.println("Initial collection value: " + list);
      // shuffle values of this collection
      Collections.shuffle(list);
      System.out.println("Final collection value: "+list);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Initial collection value: [Welcome, to, Tutorialspoint]
Final collection value: [Tutorialspoint, Welcome, to]

Getting a Shuffled List using Random Seed from a List of Integer Example

The following example shows the usage of Java Collection shuffle(List, Random) method. We've created a List object with some Integers, printed the original list. Using shuffle(List, Random) method, we've shuffled elements of the list with given Random then printed the updated list.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5));

      System.out.println("Initial collection value: " + list);
      // shuffle this collection
      Collections.shuffle(list, new Random(2L));
      System.out.println("Final collection value: "+list);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Initial collection value: [1, 2, 3, 4, 5]
Final collection value: [5, 1, 3, 2, 4]
java_util_collections.htm
Advertisements