JavaDoc of ImmutableSet
says:
Unlike
Collections.unmodifiableSet
, which is a view of a separate collection that can still change, an instance of this class contains its own private data and will never change. This class is convenient for public static final sets ("constant sets") and also lets you easily make a "defensive copy" of a set provided to your class by a caller.
But the ImmutableSet
still stores reference of elements, I couldn't figure out the difference to Collections.unmodifiableSet()
. Sample:
StringBuffer s=new StringBuffer("a");
ImmutableSet<StringBuffer> set= ImmutableSet.of(s);
s.append("b");//s is "ab", s is still changed here!
Could anyone explain it?
See Question&Answers more detail:os