... for (Foo foo : permute(fooList)) { operateOnFoo (foo); } ... private static final Random rng = new Random(); private static <E> List<E> permute (List<E> list) { List<E> answer = Lists.newArrayList (list); Collections.shuffle (answer, rng); return answer; }
But is that the implementation people would expect? If I read permute I'd assume/hope for an implementation that will over subsequent n! (assuming distinct elements) calls enumerate all permutations of the collection.
ReplyDeleteThat's why it is private and static.
ReplyDeleteIf you really thought that, wouldn't you expect a two-argument version that allowed you to select which of the n! permutations?