|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 5 of 5
List getUnmodifieableView() {
return Collections.unmodifableList( this );
}
This code will throw an UnsupportedOperationException, one of the RuntimeExceptions, if someone trys to add or remove an element from the list.
Unfortunately, the unmodifiable views of a collection are of the same type as the original collection, which hinders compile-time
type checking. Although you may pass an unmodifiable list to a method, by virtue of its type, the compiler has no way of ensuring
the collection is unchanged by the method. The unmodifiable collection is checked at runtime for changes, but this is not
quite as strong as compile-time checking and does not aid the compiler in code optimization. Perhaps it's time for Java to
emulate C++'s const and add another modifier signifying immutability of any method, class, or object.
Synchronized collections
Finally, note that none of the concrete methods mentioned thus far support multithreaded access in the manner that the Vectors and Hashtable did. In other words, none of the methods on the concrete implementations are synchronized and none of the implementations are thread-safe. You must support thread safety yourself.
This may seem like a major omission, but in actuality it's not really a big problem. The Collections class provides a synchronized version of each of the collection implementations. You ensure thread safety by using the synchronized
version of a collection and synchronizing on the returned object. For example, we can ensure thread safety on a List by using the following construct:
List dogs = synchronized List( new ArrayList() );
synchronized( list ) {
Iterator iterator = list.iterator(); // Must be in synchronized block
while ( iterator.hasNext() )
nonAtomicOperation( iterator.next() );
}
Many programmers are already using these types of synchronized blocks around Vectors and Hashtables, so the new considerations aren't too significant.
That concludes our survey of the new Java Collections Framework. We covered a lot of territory, so let's briefly review. We began with a look at the interfaces, which are used through the API and are useful for any new collections we may create. These interfaces are intuitive and provide a common way for all Java programmers to access collections. By implementing a common interface in the Collections framework, you make it easy for other programmers to access your collection, you reduce the time it takes for others to learn your class, and you make your class more useful.
We also examined the basic concrete implementations provided with the framework. You can use these basic collections to implement
any generic collection of Java objects. Like the existing Vector and Hashtable, these new collection implementations will cover a majority of your needs as a developer.
Finally, we looked at several general-purpose methods for sorting and manipulating collections. The sort interfaces are easy to add to any class, and the Collections sort methods will knock a few deliverables out of your code package.
Read more about Core Java in JavaWorld's Core Java section.