This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
I need an ThreadSafe collection.I am unable to understand the difference between unmodifiablexxx() and Singletonxx() of Collections class ,both seem to return immutable objects. So are they threadSafe ?
If I can use immutable object I donot want to use synchronized Collections (synchronizedxxx())
The unmodifiable lists are not immutable; the unmodifiable bit is slightly misleading. If the original List is changed, the copy is changed. Regard it as read-only rather than immutable.
I have never tried singletonList().
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.
You could also use the classes in the *new* java.util.concurrent.* package. With those classes there is no need for external synchronization because the classes contain useful atomic operations which would otherwise require external synchronization. An example:
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Joined: Jul 29, 2006
I am really sorry for this unavoidable delay. Please don't think otherwise.
I have a small doubt.
if(result != null) // Case another thread altered it
System.out.println("Previous value: " + result);
As I understand, this is to understand when element has been added.
But the above is about 250% faster
This is because the above code will call putifAbsent() less frequently.