views:

101

answers:

4

Does anyone know of a Java/JVM data structure library providing functional (a.k.a. immutable, or "persistent" in the functional sense) equivalents of the familiar Java data structures?

By "functional" I mean that the objects themselves are immutable, while modifications to those objects return new objects sharing the same internals as the parent object where appropriate (for efficiency in both time and space; a naïve implementation could just copy the whole thing on every write).

Much like Java's concurrency libraries, this doesn't seem like something I can or should implement myself, so it would be nice to have a functional data structure library I can use in the JVM.

+3  A: 

Try using Guava, it has immutable map, list, set. It also has some utilities to support immutable collection that instead modifying the underlying object, returns a new object.

nanda
+2  A: 

Clojure's immutable and persistent data structures have been extracted as a Java library. You can find them at http://github.com/krukow/clj-ds. These data structures are not dependent on the Clojure runtime and hence can be used without the clojure.jar in your application's classpath. They have been generified to work smoothly with Java code.

Please make a note that working with these immutable data structures may not be idiomatic in Java.

The github page does not have a jar for download. You will have to checkout the source and build the jar yourself.

abhin4v
+3  A: 

Functional and immutable are core properties of most of the Scala collection libraries. Scala compiles to the JVM and interoperates well with Java. The Scala syntax is also much closer to Java than something like Clojure (Lisp syntax).

Here's the intro page to the Scala collection API. http://www.scala-lang.org/docu/files/collections-api/collections.html

Alkaline
+1  A: 
Olathe