简体   繁体   中英

what's a good persistent collections framework for use in java?

By persistent collections I mean collections like those in clojure.

For example, I have a list with the elements (a,b,c). With a normal list, if I add d, my original list will have (a,b,c,d) as its elements. With a persistent list, when I call list.add(d), I get back a new list, holding (a,b,c,d). However, the implementation attempts to share elements between the list wherever possible, so it's much more memory efficient than simply returning a copy of the original list. It also has the advantage of being immutable (if I hold a reference to the original list, then it will always return the original 3 elements).

This is all explained much better elsewhere (eg http://en.wikipedia.org/wiki/Persistent_data_structure ).

Anyway, my question is... what's the best library for providing this functionality for use in java? Can I use the clojure collections somehow (other that by directly using clojure)?

Just use the ones in Clojure directly. While obviously you might not want to use the language it's self, you can still use the persistent collections directly as they are all just Java classes.

import clojure.lang.PersistentHashMap;
import clojure.lang.IPersistentMap;

IPersistentMap map = PersistentHashMap.create("key1", "value1");

assert map.get("key1").equals("value1");
IPersistentMap map2 = map.assoc("key1", "value1");

assert map2 != map;
assert map2.get("key1").equals("value1");

(disclaimer: I haven't actually compiled that code :)

the down side is that the collections aren't typed, ie there are no generics with them.

What about pcollections ?

You can also check out Clojure 's implementation of persistent collections ( PersistentHashMap , for instance).

I was looking for a slim, Java "friendly" persistent collection framework and took TotallyLazy and PCollections mentioned in this thread for a testdrive, because they sounded most promising to me.

Both provide reasonable simple interfaces to manipulate persistent lists:

// TotallyLazy
PersistentList<String> original = PersistentList.constructors.empty(String.class);
PersistentList<String> modified = original.append("Mars").append("Raider").delete("Raider");

// PCollections
PVector<String> original = TreePVector.<String>empty();
PVector<String> modified = original.plus("Mars").plus("Raider").minus("Raider");

Both PersistentList and PVector extend java.util.List , so both libraries should integrate well into an existing environment.

It turns out, however, that TotallyLazy runs into performance problems when dealing with larger lists (as already mentioned in a comment above by @levantpied). On my MacBook Pro (Late 2013) inserting 100.000 elements and returning the immutable list took TotallyLazy ~2000ms, whereas PCollections finished within ~120ms.

My (simple) test cases are available on Bitbucket , if someone wants to take a more thorough look.

[UPDATE] : I recently had a look at Cyclops X , which is a high performing and more complete lib targeted for functional programming. Cyclops also contains a module for persistent collections.

https://github.com/andrewoma/dexx is a port of Scala's persistent collections to Java. It includes:

  • Set, SortedSet, Map, SortedMap and Vector
  • Adapters to view the persistent collections as java.util equivalents
  • Helpers for easy construction

Functional Java implements a persistent List, lazy List, Set, Map, and Tree. There may be others, but I'm just going by the information on the front page of the site.

I am also interested to know what the best persistent data structure library for Java is. My attention was directed to Functional Java because it is mentioned in the book, Functional Programming for Java Developers .

There's pcollections (Persistent Collections) library you can use:

http://code.google.com/p/pcollections/

The top voted answer suggest to directly use the clojure collections which I think is a very good idea. Unfortunately the fact that clojure is a dynamically typed language and Java is not makes the clojure libraries very uncomfortable to use in Java.

Because of this and the lack of light-weight, easy-to-use wrappers for the clojure collections types I have written my own library of Java wrappers using generics for the clojure collection types with a focus on ease of use and clarity when it comes to interfaces.

https://github.com/cornim/ClojureCollections

Maybe this will be of use to somebody.

PS: At the moment only PersistentVector, PersistentMap and PersistentList have been implemented.

Paguro provides type-safe versions of the actual Clojure collections for use in Java 8+. It includes: List (Vector), HashMap, TreeMap, HashSet, and TreeSet. They behave exactly the way you specify in your question and have been painstakingly fit into the existing java.util collections interfaces for maximum type-safe Java compatibility. They are also a little faster than PCollections .

Coding your example in Paguro looks like this:

// List with the elements (a,b,c)
ImList<T> list = vec(a,b,c);

// With a persistent list, when I call list.add(d),
// I get back a new list, holding (a,b,c,d)
ImList<T> newList = list.append(d);

list.size(); // still returns 3

newList.size(); // returns 4

You said,

The implementation attempts to share elements between the list wherever possible, so it's much more memory efficient and fast than simply returning a copy of the original list. It also has the advantage of being immutable (if I hold a reference to the original list, then it will always return the original 3 elements).

Yes, that's exactly how it behaves. Daniel Spiewak explains the speed and efficiency of these collections much better than I could.

May want to check out clj-ds . I haven't used it, but it seems promising. Based off of the projects readme it extracted the data structures out of Clojure 1.2.0.

In the same vein as Cornelius Mund, Pure4J ports the Clojure collections into Java and adds Generics support.

However, Pure4J is aimed at introducing pure programming semantics to the JVM through compile time code checking, so it goes further to introduce immutability constraints to your classes, so that the elements of the collection cannot be mutated while the collection exists.

This may or may not be what you want to achieve: if you are just after using the Clojure collections on the JVM I would go with Cornelius' approach, otherwise, if you are interested in pursuing a pure programming approach within Java then you could give Pure4J a try.

Disclosure: I am the developer of this

totallylazy is a very good FP library which has implementations of:

  • PersistentList<T> : the concrete implementations are LinkedList<T> and TreeList<T> (for random access)
  • PersistentMap<K, V> : the concrete implementations are HashTreeMap<K, V> and ListMap<K, V>
  • PersistentSortedMap<K, V>
  • PersistentSet<T> : the concrete implementation is TreeSet<T>

Example of usage:

import static com.googlecode.totallylazy.collections.PersistentList.constructors.*;
import com.googlecode.totallylazy.collections.PersistentList;
import com.googlecode.totallylazy.numbers.Numbers;

...

PersistentList<Integer> list = list(1, 2, 3);

// Create a new list with 0 prepended
list = list.cons(0);

// Prints 0::1::2::3
System.out.println(list);

// Do some actions on this list (e.g. remove all even numbers)
list = list.filter(Numbers.odd);
// Prints 1::3
System.out.println(list);

totallylazy is constantly being maintained. The main disadvantage is the total absence of Javadoc.

I'm surprised nobody mentioned vavr. I use it for a long time now.

http://www.vavr.io

Description from their site:

Vavr core is a functional library for Java. It helps to reduce the amount of code and to increase the robustness. A first step towards functional programming is to start thinking in immutable values. Vavr provides immutable collections and the necessary functions and control structures to operate on these values. The results are beautiful and just work.

https://github.com/arnohaase/a-foundation is another port of Scala's libraries.

It is also available from Maven Central: com.ajjpj.a-foundation:a-foundation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM