Babibel
Babibel

Reputation: 15

shuffle arraylist without duplicates

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

Answers (1)

Giovanni Botta
Giovanni Botta

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

Related Questions