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 !
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.