• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is the lack of immutable collection types in Java a problem?

 
Sean Corfield
Ranch Hand
Posts: 311
13
Clojure Linux Mac OS X Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Venkat,

When I'm working with Clojure (or Scala or any number of other functional programming languages), the collection types are inherently immutable and usually optimized - e.g., persistent collections in Clojure and Scala - so that map / filter / etc are pretty efficient.

Java 8 doesn't have these optimized immutable collections so it seems you have to call stream() and collect(..) all over the place.

Do you think this will hold Java 8 back in terms of either efficiency or ease of use / ease of learning for FP style code?

Thanks,
Sean
 
Venkat Subramaniam
Author
Ranch Hand
Posts: 109
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sean,

There's no direct correlation between immutability and efficiency.

An immutable data structure can be efficient or largely inefficient when it comes to transforming from one collection of values to another. In Clojure, efficiency comes from the use of Tries and other highly efficient data structures.

In Java 8, efficiency comes from streams. That's one of the reason for the separation of stream and collect. Streams perform lazy evaluations and also tactfully postpone the creation of the target data structure.

If efficiency is the main concern, it would be good to run some benchmarks to measure the performance and then come to conclusion if one is better than the other.

The lack of immutable data structure is a concern, however, not from the point of view of efficiency, but from the point of view of errors that may arise due to inappropriate shared mutability. The anti-pattern of trying to update a data structure from within lambdas is something programmers are going to do and then learn to avoid.

Thanks

Venkat
 
Sean Corfield
Ranch Hand
Posts: 311
13
Clojure Linux Mac OS X Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

I guess my skepticism on the efficiency angle will need to be tempered by running comparative benchmarks against some transducer-based Clojure code

I'm glad you agree on the ease of use / potential for errors aspects of collections.
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought you can create an Collection in an unmodifiable form like this:-Now, unmodifiableList doesn't make a List immutable, but read‑only. That isn't a problem because you have thrown away the original reference to the ArrayList which was a local variable.
You cannot make a Collection truly immutable because all you have to do is this:-
myList.get(123).setX(456.789);
and the state of one of the elements of the List changes. It would not be possible to return defensive copies because some types don't have a clone() method or copy constructor or similar.

And sorry for coming late to this discussion.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!