Category Archives: guava

Guava Immutable Collections vs Collections.unmodifiable

Java Collections

Collections.unmodifiableList or Collections.unmodifiableSet methods create a wrapper around the original list or set. Hence any changes to the original list would affect the unmodifiable list too.

Set<String> x = new HashSet<String>();
Set<String> builtIn = Collections.unmodifiableSet(x);
System.out.println(builtIn.size()); // Prints 2

It would be more clear if you have a look at the implementation of Collections.unmodifiableList() method

Read the rest of this entry