In this article, we will learn how to shuffle elements of ArrayList & Arrays using Collections class’ utility method – shuffle()
1. Shuffling elements of ArrayList :
Method signature:
public static void shuffle(List<Object> list);
ShuffleArrayList.java
package in.bench.resources.java.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ShuffleArrayList {
public static void main(String[] args) {
// 1. create List - to store Strings
List<String> listOfStrings = new ArrayList<String>();
// 2. adding elements to ArrayList object
listOfStrings.add("Whatsup");
listOfStrings.add("Facebook");
listOfStrings.add("Orkut");
listOfStrings.add("Instagram");
listOfStrings.add("Google");
// 2.1 print to console
System.out.println("Original String List - before shuffling :- \n"
+ listOfStrings);
// 3. shuffling ArrayList using Collections.shuffle(al)
Collections.shuffle(listOfStrings);
System.out.println("\nString List - after shuffling :- \n"
+ listOfStrings);
// 4. Again, shuffling ArrayList using Collections.shuffle(al)
Collections.shuffle(listOfStrings);
System.out.print("\nString List - after shuffling twice :- \n"
+ listOfStrings);
}
}
Output:
Original String List - before shuffling :-
[Whatsup, Facebook, Orkut, Instagram, Google]
String List - after shuffling :-
[Google, Orkut, Whatsup, Instagram, Facebook]
String List - after shuffling twice :-
[Orkut, Facebook, Instagram, Google, Whatsup]
Note: shuffling every time produces different order
2. Shuffling elements of Arrays :
ShuffleArrays.java
package in.bench.resources.java.collections;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class ShuffleArrays {
public static void main(String[] args) {
// 1. String[] Arrays
String[] strArrays = {
"Whatsup",
"Facebook",
"Orkut",
"Instagram",
"Google"
};
System.out.println("Original String[] Arrays - before shuffling :- \n"
+ Arrays.toString(strArrays));
// 2. convert String[] Arrays to List
List<String> listOfStrings = Arrays.asList(strArrays);
// 3. shuffling Arrays using Collections.shuffle(al)
Collections.shuffle(listOfStrings);
System.out.println("\nString[] Arrays - after shuffling :- \n"
+ listOfStrings);
// 4. Again, shuffling Arrays using Collections.shuffle(al)
Collections.shuffle(listOfStrings);
System.out.print("\nString[] Arrays - after shuffling twice :- \n"
+ listOfStrings);
}
}
Output:
Original String[] Arrays - before shuffling :-
[Whatsup, Facebook, Orkut, Instagram, Google]
String[] Arrays - after shuffling :-
[Google, Orkut, Facebook, Instagram, Whatsup]
String[] Arrays - after shuffling twice :-
[Google, Facebook, Whatsup, Instagram, Orkut]
Note: shuffling every time produces different order
Related Articles:
- Java – Collections class a utility class for Collection
- Java – Sorting ArrayList using Comparable and Comparator
- Java – Searching element from ArrayList using Binary Search Algorithm
- Java – How to Reverse order of elements in ArrayList ?
- Java – How to Reverse order of Comparator ?
- Java – How to count duplicate elements of ArrayList ?
- Java – How to swap elements of ArrayList ?
- Java – How to copy elements of one ArrayList to another List ?
- Java – How to shuffle elements of ArrayList and Arrays ?
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/7/docs/api/java/util/List.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
Happy Coding !!
Happy Learning !!