Java Collections shuffle

Previous
Next

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.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog