It would be nice to turn objects of Java's List interface into an immutable equivalent at the point in time that mutation is no longer required. That is, the client can call a freeze
method that makes the List immutable. The immediate benefit to me is thread-safety without the memory expense of deep copying. (Edit: People would be correct if they assume that one extra immutable copy, to be used by all threads, is affordable.)
Is there a third-party interface or class that provides such a feature?
Lists that guarantee that no change in the list will ever be visible (even if their underlying list is modified) are referred to as immutable. It is worth noting that making a list final will not make it unmodifiable or immutable. We can still add elements or remove elements from it.
If you're wondering about java. util. ArrayList - it is mutable and it is not creating another List instance on add() or remove() . If you are looking for immutable list - check Guava implementation of ImmutableList or Collections.
How about Collections.unmodifiableList(List list)?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With