Java Collections shuffle

Shuffle method is to permute passed list with default or specified randomness.


Syntax

Example :
Let’s see both the function with the help of example.

When you run above program, you will get below output:

==========================
Before shuffling
[John, Martin, Mohan, Mary, Tom] ==========================
After shuffling
[Mohan, Martin, Mary, Tom, John] ==========================
After shuffling with randomness of 3
[Mary, Martin, John, Mohan, Tom]

Internal working of Java collections shuffle

Java Collections shuffle interally does Fisher-Yates shuffle.Here is the implementation of shuffle method provided by JDK.

Here is implementation of swap method.

That’s all about Java collections shuffle method.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *