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.
Given that attributes are final and that Java String are immutable, the class still safe against unwanted changes. Note that it works only because String are immutable by definition in Java. With a Date property, it wouldn't work as Date are mutable.
Immutable data structure are data structures, like lists, arrays, sets etc., which cannot be changed, meaning that the values inside them can't be added, removed, moved or swapped.
In object-oriented and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created. This is in contrast to a mutable object (changeable object), which can be modified after it is created.
Immutable data structures provides referential transparency which makes it easier to reason about our program locally. Another way to think about it is that every time we execute a pure (referentially transparent) function with the same input, we get the same output.
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.
Try Functional Java. It contains immutable maps, sets, lists, and trees. However, this library is much more than just a collection of immutable data structures!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With