13

I am looking for a library for java that will generate all possible order permutations of a set. The only library I can find is combinatoricslib on google code. I find it very hard to believe this is the only java library that does this, and am quite frankly very surprised by this.

Is there anything in the JDK, or apache commons math, or another library, that provides this same functionality?

I am happy to use combinatoricslib, I just can't believe that's the only option, other than writing the algorithm myself, which admittedly is not that difficult, but neither is .isBlankOrNull(), and apache commons includes that.

Paul Sanwald
  • 10,899
  • 6
  • 44
  • 59
  • FYI, as a general rule, "is it in a library?" is less strongly correlated to "difficulty" and more strongly correlated to "how many people actually encounter this use case regularly in real-world problems." – Louis Wasserman Aug 17 '12 at 22:16
  • @LouisWasserman:If this is accurate then why isn't there any library for `tree` or `trie` datastructures? – Cratylus Aug 18 '12 at 16:42
  • I think my above statement answers that question, too, though it's also the case that tries are such a general structure that making one library that fits all use cases is more or lesss impossible. – Louis Wasserman Aug 18 '12 at 16:51

2 Answers2

13

Have you checked Guava? It seems to offer permutations in Collections2

Class Collections2
permutations(Collection elements)
Returns a Collection of all the permutations of the specified Collection.

Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31
Cratylus
  • 52,998
  • 69
  • 209
  • 339
4

There's the PermutationIterator class in the Apache Commons Collection library version 4. It accepts a generic Collection and provides an Iterator that iterates over all permutations of the elements in the Collection (each call to next() on the Iterator provides an ordered List).

I just realised version 4 of the Collection library was only released on 2013-11-27. Version 3 doesn't provide a utility for permutations. V4 "represents a major revamp of collections by supporting new language features introduced with Java 1.5, mainly support for generics."

Oliver Coleman
  • 734
  • 7
  • 13