shuffle List

0
Hi, Greetings!   I'm trying to create a Java action that accepts a List and returns a shuffled list.   I have tried using the Collections.shuffle(ListName);, but it didn't work.   Does anyone have any idea how to shuffle a list?   Thank you.
asked
1 answers
2

The collection.shuffle does the trick.

I have tested this with an entity (card) containing one attribute. Created several records (A1, A2, A3,B1, B2, B3).

In a microflow I retrieve the list from the database and sort on the attribute so that the list reads like above in order. The passed the list to the java action that has the following code:

 

Collections.shuffle(__cards);        

return __cards;

And as the input parameter takes a list and the output parameter is a list as well.

Then I used the output list in the microflow as the source for a loop and logged one line for every item in the list and see that the list is shuffled.

Also tried this with multiple attributes and then this works as well. Be aware though that the shuffle method does not ensure that the list is returned in a different order as the method takes a random item from the list and permutates over this and this can result in the same list order.

Rewriting the shuffle call with a higher level of randomness might help to prevent this from happening too often. Then write:

Collections.shuffle(__cards, new Random(5)); e.g.

answered