4 ms·
You can't add items to immutable collections - if you call add() etc you'll get an UnsupportedOperationException. If you want a mutable version then you need t
by akronim 17y ago
You can't add items to immutable collections - if you call add() etc you'll get an UnsupportedOperationException. If you want a mutable version then you need to create a new collection, and can use the immutable collection to provide the starting values, like this:
Set<String> mutable = new HashSet<String>(immutable)
- paulbaumgart 17y agoOh, but you can still do: List<String> list = new ArrayList<String>(); list.add("item"); String item = list[0]; right? I guess I was thrown off by the wording- I assumed the line about collections being immutable applied to all collections that use the new syntax.
- JulianMorrison 17y agoWhich is madness that completely misses the point of proper immutable collections (Clojure style) - that you can "modify" them, by creating a new copy with the thing added, and it shares storage with the original and the copying isn't as slow as a copy-by-clone. The first thing people are going to do with these half assed immutable collections is pour the contents out into a mutable container. Which completely obviates all the thread safety and whatnot. I slap my forehead. Do these people have no sense?