How to shuffle the elements of a collection?
SolutionFollowing example how to shuffle the elements of a collection with the help of Collections.shuffle() method of Collections class.
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main { public static void main(String[] argv) throws Exception { ArrayList<String> obj = new ArrayList<String>(); obj.add("A"); obj.add("E"); obj.add("I"); obj.add("O"); obj.add("U"); Collections.shuffle(obj); System.out.println(obj); } }Result
The above code sample will produce the following result.
[I, U, A, O, E]
java_collections.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4