Java ArrayList shuffle elements example shows how to shuffle elements of ArrayList in Java. The example also shows how to shuffle ArrayList using Random seed using the Collections class.
How to shuffle elements of ArrayList?
To shuffle elements of ArrayList, use shuffle
method of Collections class.
1 |
public static void shuffle(List<?> list) |
This method shuffles ArrayList using the default source of randomness. Internally, it iterates the List in backward direction till the second element and swaps the random element to the current position.
Java ArrayList shuffle example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; import java.util.Collections; public class ArrayListShuffleExample { public static void main(String[] args) { ArrayList<String> aListNumbers = new ArrayList<String>(); aListNumbers.add("One"); aListNumbers.add("Two"); aListNumbers.add("Three"); aListNumbers.add("Four"); aListNumbers.add("Five"); //print original List System.out.print("Original ArrayList: "); System.out.println(aListNumbers); /* * To Shuffle ArrayList, use * shuffle method of Collections class */ Collections.shuffle(aListNumbers); //print shuffled ArrayList System.out.print("Shuffled ArrayList: "); System.out.println(aListNumbers); } } |
Output
1 2 |
Original ArrayList: [One, Two, Three, Four, Five] Shuffled ArrayList: [Two, One, Four, Five, Three] |
How to shuffle two ArrayList in the same sequence?
Imagine that you have two ArrayList objects, one contains the keys and the second ArrayList contains the corresponding values. How to shuffle keys and still be able to retrieve corresponding values from the second list? Consider below given example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
ArrayList<Integer> aListKeys = new ArrayList<Integer>(); aListKeys.add(1); aListKeys.add(2); aListKeys.add(3); aListKeys.add(4); aListKeys.add(5); ArrayList<String> aListValues = new ArrayList<String>(); aListValues.add("One"); aListValues.add("Two"); aListValues.add("Three"); aListValues.add("Four"); aListValues.add("Five"); //shuffle two ArrayList objects Collections.shuffle(aListKeys); Collections.shuffle(aListValues); //print them System.out.println(aListKeys); System.out.println(aListValues); |
Output
1 2 |
[4, 3, 1, 2, 5] [Four, Two, One, Three, Five] |
As you can see from the output, key-value pairs are no longer in the same order after shuffling them. How to shuffle two ArrayList using the same randomness? Well, in this case, use the overloaded shuffle
method which accepts Random as a parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
public static void shuffle(List<?> list, Random random) This method shuffles the list using specified random. package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; import java.util.Collections; import java.util.Random; public class ArrayListShuffleExample { public static void main(String[] args) { ArrayList<Integer> aListKeys = new ArrayList<Integer>(); aListKeys.add(1); aListKeys.add(2); aListKeys.add(3); aListKeys.add(4); aListKeys.add(5); ArrayList<String> aListValues = new ArrayList<String>(); aListValues.add("One"); aListValues.add("Two"); aListValues.add("Three"); aListValues.add("Four"); aListValues.add("Five"); long lRandomSeed = System.currentTimeMillis(); //shuffle both the list using same seed Collections.shuffle(aListKeys, new Random(lRandomSeed)); Collections.shuffle(aListValues, new Random(lRandomSeed)); //print both the lists System.out.println(aListKeys); System.out.println(aListValues); } } |
Output
1 2 |
[2, 3, 1, 5, 4] [Two, Three, One, Five, Four] |
As you can see from the output, now both the List objects are shuffled using the same randomness.
This example is a part of the Java ArrayList tutorial.
Please let me know your views in the comments section below.