How to Shuffle or randomize an ArrayList in Java


Here we may get the method to shuffle an ArrayList.

Source Code

package com.beginner.examples;

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

public class ShuffleArrayList {

	public static void main(String[] args) {
		
		List list = Arrays.asList("X", "Y", "Z", "10", "20", "30");
		System.out.println("Before shuffle :" + list);
		
		Collections.shuffle(list);
		System.out.println("After shuffle : " + list);

	}

}

Output:

Before shuffle :[X, Y, Z, 10, 20, 30]
After shuffle : [30, Z, 10, 20, X, Y]

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments