Java Collections shuffle() MethodThe shuffle() is a Java Collections class method which works by randomly permuting the specified list elements. There is two different types of Java shuffle() method which can be differentiated depending on its parameter. These are:
Java Collections shuffle(list) MethodThe shuffle(list) method is used to work by randomly reorders the specified list elements using a default randomness. Java Collections shuffle(list, random) MethodThe shuffle(list, random) method is used to work by randomly reorders the list elements using the specified randomness. SyntaxFollowing is the declaration of shuffle() method: Parameter
ReturnsThe shuffle() method does not return anything. ExceptionsUnsupportedOperationException- This method thrown exception if the specified list or its list-iterator does not support the set operation. Compatibility VersionJava 1.5 and above Example 1Test it NowOutput: List before Shuffle : [A, B, C, D] List after shuffle : [A, C, D, B] Example 2Test it NowOutput: List before Shuffle : [10, -20, 50, 90, -15] List after shuffle : [10, 50, 90, -15, -20] Example 3Test it NowOutput: List before Shuffle = [45, 20, 55, 90, 15] Shuffled List with Random() = [45, 55, 15, 90, 20] Shuffled List with Random(3) = [90, 55, 45, 15, 20] Example 4Test it NowOutput: [one, two, three, four] [four, two, one, three] Next TopicJava Collections Class |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India