Collections.shuffle() in Java with Examples

2.2

java.util.Collections.shuffle() is a java.util.Collections class method.

// Shuffles mylist
public static void shuffle(List mylist)
This method throws UnsupportedOperationException if the 
given list or its list-iterator does not support 
the set operation.
// Java program to demonstrate working of shuffle()
import java.util.*;

public class GFG
{
    public static void main(String[] args)
    {
        ArrayList<String>  mylist = new ArrayList<String>();
        mylist.add("code");
        mylist.add("quiz");
        mylist.add("geeksforgeeks");
        mylist.add("quiz");
        mylist.add("practice");
        mylist.add("qa");

        System.out.println("Original List : \n" + mylist);

        Collections.shuffle(mylist);

        System.out.println("\nShuffled List : \n" + mylist);
    }
}

Output:

 
Original List : 
[code, quiz, geeksforgeeks, quiz, practice, qa]

Shuffled List : 
[qa, quiz, practice, code, quiz, geeksforgeeks]

 

shuffle(mylist, rndm)

It shuffles a given list using the user provided source of randomness.

// mylist is the list to be shuffled.
// rndm is source of randomness to shuffle the list.
public static void shuffle(List mylist, Random rndm)

It throws  UnsupportedOperationException if the specified list or
its list-iterator does not support the set operation.
// Java program to demonstrate working of shuffle()
// with user provided source of randomness.
import java.util.*;

public class GFG
{
    public static void main(String[] args)
    {
        ArrayList<String>  mylist = new ArrayList<String>();
        mylist.add("code");
        mylist.add("quiz");
        mylist.add("geeksforgeeks");
        mylist.add("quiz");
        mylist.add("practice");
        mylist.add("qa");

        System.out.println("Original List : \n" + mylist);

        // Here we use Random() to shuffle given list.
        Collections.shuffle(mylist, new Random());
        System.out.println("\nShuffled List with Random() : \n"
                                                     + mylist);

        // Here we use Random(3) to shuffle given list.
        Collections.shuffle(mylist, new Random(3));
        System.out.println("\nShuffled List with Random(3) : \n"
                                                     + mylist);

        // Here we use Random(3) to shuffle given list.
        Collections.shuffle(mylist, new Random(5));
        System.out.println("\nShuffled List with Random(5) : \n"
                                                     + mylist);

    }
}

Output:

Original List : 
[code, quiz, geeksforgeeks, quiz, practice, qa]

Shuffled List with Random() : 
[qa, quiz, geeksforgeeks, code, practice, quiz]

Shuffled List with Random(3) : 
[quiz, code, quiz, practice, qa, geeksforgeeks]

Shuffled List with Random(5) : 
[code, practice, geeksforgeeks, qa, quiz, quiz]

Important Points:

  • This method works by randomly permuting the list elements
  • Runs in linear time. If the provided list does not implement the RandomAccess interface, like LinkedList and is large, it first copies the list into an array, then shuffles the array copy, and finally copies array back into the list. This makes sure that the time remains linear.
  • It traverses the list backwards, from the last element up to the second, repeatedly swapping a randomly selected element into the “current position”. Elements are randomly selected from the portion of the list that runs from the first element to the current position, inclusive. [Source : javadoc]

Reference:
https://developer.microej.com/javadoc/microej_4.0/addons/java/util/Collections.html#shuffle-java.util.List-

This article is contributed by Mohit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2.2 Average Difficulty : 2.2/5.0
Based on 5 vote(s)