Reputation: 15
I want to shuffle an arraylist in java but using this creates duplicates :
java.util.Collections.shuffle(this.OrderedCustomers);
Here, this.OrderedCustomers is an array list.
Thanks for your help !
Upvotes: 0
Views: 923
Reputation: 9816
The Collections.shuffle
method will shuffle the elements in the given list. So if that list contains duplicates, so will the shuffled one. Add your objects to a set (assuming they implement hashCode
and equals
!) to remove duplicates first and then to a list and finally to the shuffle method.
Upvotes: 2