Does the Java standard library have any functional data structures, like immutable Sets, Lists, etc., with functional update?
-
Do you want a deep copy of all the objects in the list or simply a function that gives you a new list of pointers all to the same objects as the source?Nathan Feger– Nathan Feger2009-11-05 20:51:34 +00:00Commented Nov 5, 2009 at 20:51
-
I want a persistent data structure for a Set not implemented in a silly way. For example, an immutable list could implement adding by copying all the elements into a new list and adding the element - O(n). Or it could be a linked list, add the element to the head, and return the head - O(1)Claudiu– Claudiu2009-11-05 20:59:48 +00:00Commented Nov 5, 2009 at 20:59
-
there is no general way to "deep-copy" anything in Java.Kevin Bourrillion– Kevin Bourrillion2009-11-05 22:50:58 +00:00Commented Nov 5, 2009 at 22:50
10 Answers
Have a look at the pcollections project:
PCollections serves as a persistent and immutable analogue of the Java Collections Framework. This includes efficient, thread-safe, generic, immutable, and persistent stacks, maps, vectors, sets, and bags, compatible with their Java Collections counterparts.
Persistent and immutable datatypes are increasingly appreciated as a simple, design-friendly, concurrency-friendly, and sometimes more time- and space-efficient alternative to mutable datatypes.
Comments
Sounds like you're looking for Scala. It compiles to .class, so that's good enough, right?
1 Comment
Well, there are two possible approaches to "changing" an immutable collection:
Make a copy of it that includes the "change"
Create a new, different object that consists of a reference to the original object and a reference to a description of the change.
Clojure takes the latter approach, so it becomes fairly quick to create a lot of siblings of an original collection with minor corrections to each, with reasonable memory requirements. But most Java code tends to go for the first option.
For what it's worth, Google has created a handful of collections that support functional-style programming: http://code.google.com/p/google-collections/ but I haven't looked at them in depth.
Comments
You don't need scala. Just pass your collection into:
java.util.Collections.unmodifiableCollection(/* Collection<? extends T> c */);
java.util.Collections.unmodifiableSet(Set s);
java.util.Collections.unmodifiableMap(Map m);
java.util.Collections.unmodifiableList(List l);
I just saw this from another SO question:
Google's ImmutableSet
from the docs:
Unlike Collections.unmodifiableSet(java.util.Set), which is a view of a separate collection that can still change, an instance of this class contains its own private data and will never change. This class is convenient for public static final sets ("constant sets") and also lets you easily make a "defensive copy" of a set provided to your class by a caller.
edited to incorporate comment.
4 Comments
unmodifiableList(), unmodifiableMap(), ans more methods available in java.util.Collections API: java.sun.com/javase/6/docs/api/java/util/Collections.htmlfj.data.List if you don't understand what I mean.)I know this is an old question but a bit of searching around tells me that now we have an alternative for Functional Java.
JavasLang looks like an interesting library for declarative programming and functional data structures in Java.
I haven't compared it with Functional Java in terms of ease of use and performance, but I'd love to get any pointers on that.
Comments
If you are interested in collections manipulation in a functional style give a look to lambdaj
Comments
Strings and numbers are immutable in a functional way, but most collections are not (the immutable collections throw exceptions on add, remove, etc). CopyOnWriteArrayList and CopyOnWriteArraySet are the closest in that sense.