views:

333

answers:

8

Does the Java standard library have any functional data structures, like immutable Sets, Lists, etc., with functional update?

+1  A: 

Sounds like you're looking for Scala. It compiles to .class, so that's good enough, right?

Stefan Kendall
hmm perhaps. It's just that we got some support code in a course that acts functionally (i.e. adding something to a structure returns a new version w/ the update), but it's written by just always copying the old structure and adding something new to it. it makes me sad. i wondered if there was an easy, better way, but I guess Java just makes it hard.
Claudiu
+2  A: 

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

http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/ImmutableSet.html

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.

Nathan Feger
+1: You were quickier =) There are by the way also `unmodifiableList()`, `unmodifiableMap()`, ans more methods available in `java.util.Collections` API: http://java.sun.com/javase/6/docs/api/java/util/Collections.html
BalusC
I want to add something to a Set, keeping the old Set the same, and returning a new Set. I dont think unmodifiableSet gives me that?
Claudiu
@Claudiu yes, unmodifiableSet will certainly not do that.
Nathan Feger
Immutable and unmodifiable are not the same thing. An attempt to mutate an unmodifiable collection will lead to a RuntimeException, whereas with immutable collection such a mutation is not possible at all and thus the code is guaranteed not to blow up at runtime. (Look at source code of `fj.data.List` if you don't understand what I mean.)
missingfaktor
+2  A: 

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.

Carl Smotricz
A: 

Take a look at Google collections.

Gaël Marziou
A: 

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.

Kathy Van Stone
+11  A: 

Functional java has Sets, Lists and more interesting abstractions.

Thomas Jung
+1 for being the only correct answer here.
Kevin Bourrillion
neat, this looks promising. thanks!
Claudiu
+2  A: 

It's always nice to see Google Collections plugged, but no, we do not have this. I don't know of any Java library that does. Inside Google, we implemented some functional List structures, and guess what? No one uses them. So they aren't likely to become open-sourced any time soon.

Kevin Bourrillion
A: 

If you are interested in collections manipulation in a functional style give a look to lambdaj

Mario Fusco