I just got bitten by the collections framework. I always thought that the
Collections.unmodifiableX() methods returned an unmodifiable copy of the supplied collection, like a shallow copy. This is useful for getter methods where you want to return a copy of a collection to a client class so they can't directly alter the contents of that collection. This enforces encapsulation and ensures that clients use the methods that you have specified to manipulate your data.
Collections.unmodifiableX() methods don't work like that. Instead, they present an unmodifiable view of the specified collection. If you modify the underlying collection, this shows up in the unmodifiable view too. I won't be making that mistake again. To make a shallow copy of a collection, you can either use the
clone() methods on the collection implementation classes or construct a new list/set/map, passing the original collection as an argument to the constructor.
It pays to read the javadocs, but without a unit test I would have spent even longer hunting this problem down.