Hacker News new | ask | show | jobs
by vips7L 2093 days ago
I'm not really obsessed with immutability. Does Collections.unmodifiable{List,Set,Map,Collection} not do it for you?

I did forget about the cache's though! Good call.

1 comments

Guava's immutable collections are superior to Collections.unmodifiable which just wraps the collection in a delegating class that will throw an exception if any of the modifying methods are called. Guava's classes are their own implementations, this is particularly notable for Guava's ImmutableSet which has significantly better memory usage than HashSet. Partially owing to how lazily Java's HashSet is defined, which is a wrapper around Java's HashMap. Meaning that for each element in a HashSet you get an unnecessary Map.Entry wrapper, along with its references. The difference is quite noticeable if you have a set with ~400k Integers. At a certain point, you should also move on to something like Trove, but the guava immutable classes are nice in that you still get the collections interfaces.