- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Collections shuffle() Method
Description
The Java Collections shuffle(List<?>) method is used to randomly permute the specified list using a default source of randomness.
Declaration
Following is the declaration for java.util.Collections.shuffle() method.
public static void shuffle(List<?> list)
Parameters
list − The list to be shuffled.
Return Value
NA
Exception
UnsupportedOperationException − Throws if the specified list or its list-iterator does not support the set operation.
Java Collections shuffle(List<?>, Random) Method
Description
The Java Collections shuffle(List<?>, Random) method is used to randomly permute the specified list using the specified source of randomness.
Declaration
Following is the declaration for java.util.Collections.shuffle() method.
public static void shuffle(List<?> list,Random rnd)
Parameters
list − The list to be shuffled.
rnd − The source of randomness to use to shuffle the list.
Return Value
NA
Exception
UnsupportedOperationException − This is thrown if the specified list or its list-iterator does not support the set operation.
Getting a Shuffled List from a List of Integer Example
The following example shows the usage of Java Collection shuffle(List) method. We've created a List object with some integers, printed the original list. Using shuffle(List) method, we've shuffled elements of the list and then printed the updated list.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5)); System.out.println("Initial collection value: " + list); // shuffle this collection Collections.shuffle(list); System.out.println("Final collection value: "+list); } }
Output
Let us compile and run the above program, this will produce the following result −
Initial collection value: [1, 2, 3, 4, 5] Final collection value: [5, 1, 2, 4, 3]
Getting a Shuffled List from a List of String Example
The following example shows the usage of Java Collection shuffle(List) method. We've created a List object with some strings, printed the original list. Using shuffle(List) method, we've shuffled elements of the list and then printed the updated list.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<String> list = new ArrayList<>(Arrays.asList("Welcome","to","Tutorialspoint")); System.out.println("Initial collection value: " + list); // shuffle values of this collection Collections.shuffle(list); System.out.println("Final collection value: "+list); } }
Output
Let us compile and run the above program, this will produce the following result −
Initial collection value: [Welcome, to, Tutorialspoint] Final collection value: [Tutorialspoint, Welcome, to]
Getting a Shuffled List using Random Seed from a List of Integer Example
The following example shows the usage of Java Collection shuffle(List, Random) method. We've created a List object with some Integers, printed the original list. Using shuffle(List, Random) method, we've shuffled elements of the list with given Random then printed the updated list.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Random; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5)); System.out.println("Initial collection value: " + list); // shuffle this collection Collections.shuffle(list, new Random(2L)); System.out.println("Final collection value: "+list); } }
Output
Let us compile and run the above program, this will produce the following result −
Initial collection value: [1, 2, 3, 4, 5] Final collection value: [5, 1, 3, 2, 4]
To Continue Learning Please Login
Login with Google