java.util.Collections class provides shuffle() method which can be used to randomize object stored in a List in Java. Since List is an ordered collection and maintains the order on which objects are inserted into it, you may need to randomize elements if you need them in different order. Collections.shuffle() method uses default randomness to randomize element but you also has an overloaded version of shuffle() to provide an instance of java.util.Random object, which can be used to randomize elements. Since this method except a List, you can also pass it to LinkedList, Vector, CopyOnWriteArrayList and others, which doesn't implement RandomAccess method. In such cases, this method convert list to array before shuffling to avoid quadratic performance by shuffling sequential access list. Once shuffling is done it also converts back array to list. Shuffling has many usage e.g. shuffling deck of cards in a poker game simulation. You can also use shuffling to roll dice if you are developing any board game which requires dice e.g. Ludo.
Read more »
No comments:
Post a Comment