posted 14 years ago
The lists that Collections.unmodifiableList() and Collections.singletonList() return are two completely different things.
unmodifiableList returns a wrapper around the original list that you put in, which provides a read-only view on the underlying list.singletonList creates a new, read-only list with a single element in it.
The obvious difference is that singletonList returns a list with only one element in it. The list that singletonList returns is optimized; it always has one element, so methods such as size() can be implemented very simply with return 1;, for example.
But unmodifiableList and singletonList both don't have anything to do with synchronized collections.
By default, collection classes such as ArrayList and HashMap do not have any synchronization (because you rarely need this, and it does add a performance overhead). If you really need a synchronized list, for example, you can wrap it using Collections.synchronizedList():
If your collection is immutable, you do not need synchronization, because no threads will be able to modify it.
What Campbell says is true; Collections.unmodifiableList() is only a view on an existing list, you could still change the existing list, and you'll see the changes also in the view that unmodifiableList returned. However, if you throw away any references to the original list (and only keep the wrapped list that unmodifiableList returned), then for all practical purposes your list will be immutable.