Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (e.g. 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)?

like image 349
bm212 Avatar asked Dec 20 '11 12:12

bm212


People also ask

Which is the best Collection to use in Java?

In most situations, an ArrayList is preferred over a LinkedList . LinkedList : A List backed by a set of objects, each linked to its "previous" and "next" neighbors. A LinkedList is also a Queue and Deque .

What are the types of collections allowed by the Java collections framework?

Three Types of Collection There are three generic types of collection: ordered lists, dictionaries/maps, and sets. Ordered lists allows the programmer to insert items in a certain order and retrieve those items in the same order.

What is Java collection framework needed?

Java Collection Framework enables the user to perform various data manipulation operations like storing data, searching, sorting, insertion, deletion, and updating of data on the group of elements.


1 Answers

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, i.e. there are no generics with them.

like image 195
Gareth Davis Avatar answered Sep 19 '22 15:09

Gareth Davis