Reputation: 1114
I need to get all elements from set in random order. I found some solutions, however i would like to find the best and the fastest. Is there any analogue for Collections.shuffle?
Upvotes: 1
Views: 1336
Reputation: 3314
There's not a direct analogue, because there's no order on sets, so shuffling has no semantics for the set. If you want to get the elements in different orders a number of times, you need to convert the Set to a list (which you know has no duplicates because it just came from a Set) and shuffle it.
List<Elem> withAnOrdering = new ArrayList<>(fromMySetOfElem);
for (int i = 0; i < numTimes; i++)
{
Collections.shuffle(withAnOrdering);
//Do something with the ordering
}
Upvotes: 4