Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functional/Immutable Data Structures for the JVM? [closed]

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.

like image 708
Pierre-Évariste Avatar asked Oct 01 '10 14:10

Pierre-Évariste


People also ask

Which data structures are immutable in Java?

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.

Which data structure is immutable?

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.

What is immutable data in functional programming?

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.

Why do we use immutable data structures?

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.


3 Answers

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.

like image 123
Abhinav Sarkar Avatar answered Sep 29 '22 12:09

Abhinav Sarkar


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!

like image 41
Bolo Avatar answered Sep 29 '22 12:09

Bolo


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

like image 21
Bernard Avatar answered Sep 29 '22 11:09

Bernard