From Java 1.6 Collection Framework documentation:
Collections that do not support any modification operations (such as
add
,remove
andclear
) are referred to as unmodifiable. [...] Collections that additionally guarantee that no change in the Collection object will ever be visible are referred to as immutable.
The second criteria confuses me a bit. Given the first collection is 开发者_JS百科unmodifiable, and assuming that the original collection reference has been disposed away, what are the changes that are referred to in the second line? Is it referring to the changes in the elements held in the collection ie the state of the elements?
Second question:
For a collection to be immutable, how does one go about providing the additional guarentees specified? If the state of an element in the collection is updated by a thread, is it sufficient for immutability that those updates in the state are not visible on the thread holding the immutable collection?For a collection to be immutable, how does one go about providing the additional guarantees specified?
Unmodifiable collections are usually read-only views (wrappers) of other collections. You can't add, remove or clear them, but the underlying collection can change.
Immutable collections can't be changed at all - they don't wrap another collection - they have their own elements.
Here's a quote from guava's ImmutableList
Unlike
Collections.unmodifiableList(java.util.List<? extends T>)
, which is a view of a separate collection that can still change, an instance ofImmutableList
contains its own private data and will never change.
So, basically, in order to get an immutable collection out of a mutable one, you have to copy its elements to the new collection, and disallow all operations.
The difference is that you can't have a reference to an immutable collection which allows changes. Unmodifiable collections are unmodifiable through that reference, but some other object may point to the same data through which it can be changed.
e.g.
List<String> strings = new ArrayList<String>();
List<String> unmodifiable = Collections.unmodifiableList(strings);
unmodifiable.add("New string"); // will fail at runtime
strings.add("Aha!"); // will succeed
System.out.println(unmodifiable);
Collection<String> c1 = new ArrayList<String>();
c1.add("foo");
Collection<String> c2 = Collections.unmodifiableList(c1);
c1
is mutable (i.e. neither unmodifiable nor immutable).
c2
is unmodifiable: it can't be changed itself, but if later on I change c1
then that change will be visible in c2
.
This is because c2
is simply a wrapper around c1
and not really an independent copy. Guava provides the ImmutableList
interface and some implementations. Those work by actually creating a copy of the input (unless the input is an immutable collection on its own).
Regarding your second question:
The mutability/immutability of a collection does not depend on the mutability/immutability of the objects contained therein. Modifying an object contained in a collection does not count as a "modification of the collection" for this description. Of course if you need a immutable collection, you usually also want it to contain immutable objects.
Now java 9 has factory Methods for Immutable List, Set, Map and Map.Entry .
In Java SE 8 and earlier versions, We can use Collections class utility methods like unmodifiableXXX to create Immutable Collection objects.
However these Collections.unmodifiableXXX methods are very tedious and verbose approach. To overcome those shortcomings, Oracle corp has added couple of utility methods to List, Set and Map interfaces.
Now in java 9 :- List and Set interfaces have “of()” methods to create an empty or no-empty Immutable List or Set objects as shown below:
Empty List Example
List immutableList = List.of();
Non-Empty List Example
List immutableList = List.of("one","two","three");
I believe the point here is that even if a collection is Unmodifiable, that does not ensure that it cannot change. Take for example a collection that evicts elements if they are too old. Unmodifiable just means that the object holding the reference cannot change it, not that it cannot change. A true example of this is Collections.unmodifiableList
method. It returns an unmodifiable view of a List. The the List reference that was passed into this method is still modifiable and so the list can be modified by any holder of the reference that was passed. This can result in ConcurrentModificationExceptions and other bad things.
Immutable, mean that in no way can the collection be changed.
Second question: An Immutable collection does not mean that the objects contained in the collection will not change, just that collection will not change in the number and composition of objects that it holds. In other words, the collection's list of references will not change. That does not mean that the internals of the object being referenced cannot change.
Pure4J supports what you are after, in two ways.
First, it provides an @ImmutableValue
annotation, so that you can annotate a class to say that it is immutable. There is a maven plugin to allow you to check that your code actually is immutable (use of final
etc.).
Second, it provides the persistent collections from Clojure, (with added generics) and ensures that elements added to the collections are immutable. Performance of these is apparently pretty good. Collections are all immutable, but implement java collections interfaces (and generics) for inspection. Mutation returns new collections.
Disclaimer: I'm the developer of this
Before Java 9, Collections.unmodifiableXXX() methods are used to create unmodifiable collections. These methods just behave like wrapper methods which return unmodifiable view or read-only view of the original collection. i.e you can’t perform modifying operations like add, remove, replace, clear etc through the references returned by these wrapper methods. But, you can modify original collection if you have other references to it and those modifications will be reflected in the view returned by these methods.
For example,
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Java9ImmutableCollections
{
public static void main(String[] args)
{
List<String> sportList = new ArrayList<String>();
sportList.add("Hockey");
sportList.add("Cricket");
sportList.add("Tennis");
List<String> unModifiableSportList = Collections.unmodifiableList(sportList);
System.out.println(sportList); //Output : [Hockey, Cricket, Tennis]
System.out.println(unModifiableSportList); //Output : [Hockey, Cricket, Tennis]
unModifiableSportList.add("Wrestling"); //It gives run-time error
sportList.add("Kabaddi"); //It gives no error and will be reflected in unModifiableSportList
System.out.println(sportList); //Output : [Hockey, Cricket, Tennis, Kabaddi]
System.out.println(unModifiableSportList); //Output : [Hockey, Cricket, Tennis, Kabaddi]
}
}
From Java 9, static factory methods are introduced to create immutable collections.
1) Immutable List : List.of()
2) Immutable Set : Set.of()
3) Immutable Map : Map.of() or Map.ofEntries()
Immutable Vs Unmodifiable :
Java 9 Immutable collections and unmodifiable collections returned by the Collections.unmodifiableXXX() wrapper methods are not the same. Unmodifiable collections are just the read-only views of the original collection. You can perform modifying operations on the original collection and those modifications will be reflected in the collections returned by these methods. But, immutable collections returned by Java 9 static factory methods are 100% immutable. You can’t modify them once they are created.
Source : https://javaconceptoftheday.com/java-9-immutable-collections/
精彩评论