That is, immutable but data sharing with effectively O(1) indexing.
-
See also http://stackoverflow.com/questions/8575723/whats-a-good-persistent-collections-framework-for-use-in-java – om-nom-nom Apr 24 '13 at 08:36
4 Answers
Karl Krukow extracted clojure data structures into standalone library, so you can use it without bringing whole clojure to your project. There is also pcollections which has TreePVector implementation (with logarithmic time lookups).

- 62,329
- 13
- 183
- 228
I made a library of persistent data structures for Java a couple of years back that may fit the bill:
https://github.com/mikera/mikera/tree/master/src/main/java/mikera/persistent
They are somewhat like the Clojure data structures, but with more of a Java flavour:
- Make full use of generics
- Support all the Java collection interfaces you would expect
- Include some handy specialised types (e.g.
RepeatList
for repeated occurences of the same value)

- 105,238
- 25
- 256
- 415
-
I use them a lot (whenever I don't want to pull in the whole of Clojure...), and maintain the code as open source. I think a few other people are using them. Basically, they are in a robust and usable state. – mikera Apr 14 '13 at 23:46
Remember that Clojure, in the end, is just Java. As such you can just put Clojure's jar in your classpath and use its classes.
For instance, Clojure vectors are instances of clojure.lang.PersistentVector
, maps are instances of clojure.lang.PersistentArrayMap
and lists, instances of clojure.lang.PersistentList
.
I haven't tried this in anger myself but that's probably the line I'd take if I wanted to use persistent data structures in Java.
Something like this could get you started:
import clojure.lang.PersistentVector;
...
ArrayList list = ...
PersistentVector myVector = PersistentVector.create(list);
// from here on, using myVector takes advantages of its persistent nature.
Obviously these classes were built with Clojure's API in mind, and not ease of use from Java - but it's certainly possible.

- 5,579
- 1
- 24
- 26
-
Thanks, but that is a heavyweight solution, and as you say the ease of use from Java is compromised. – Duncan McGregor Apr 14 '13 at 12:25
-
-
Having spent a year in Scala, not having what is effectively the default Scala collection is a bit painful! Guava helps with some functional constructs, but it's immutable collections are poor compared to Vector. – Duncan McGregor Apr 14 '13 at 19:54