Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

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

Get started with the Java Collections Framework

Find out how Sun's new offering can help you to make your collections more useful and accessible

  • Print
  • Feedback

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.

Conclusion

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.

About the author

Dan Becker works in the Network Computing Software Division of IBM Corporation in Austin, TX. He is currently working on Java 1.2 and other Java extensions for IBM Operating System/2. Before that, Dan worked on porting previous Java virtual machines, the multimedia plugins for Netscape Navigator for OS/2, OpenDoc, and the Multimedia parts for OS/2 Warp Version 4.0.

Read more about Core Java in JavaWorld's Core Java section.

  • Print
  • Feedback

Resources
  • Download the Java Collections Framework as package for Java 1.1 JDK 1.1 at the Sun JavaBeans InfoBus page http://java.sun.com/beans/infobus/index.html
  • Sun's overview and description of the Java Collections Framework for Java 1.2 http://java.sun.com/products/jdk/1.2/docs/guide/collections/index.html
  • Annotated reference of the Java Collections Framework http://java.sun.com/products/jdk/1.2/docs/guide/collections/reference.html
  • A frequently asked questions file for the Java Collections Framework. http://java.sun.com/products/jdk/1.2/docs/guide/collections/designfaq.html
  • Read about ObjectSpace's Java Generic Library -- the main competitor to the Java Collections Framework http://www.objectspace.com/products/jgl/