Legacy Collections


Prior to the SDK version 1.2, there was no collections "framework" in Java. Java contained two main workhorse collectionsjava.util.Vector and java.util.Hashtable[6]that still exist today. The analogous classes in modern Java are respectively java.util.ArrayList and java.util.HashMap. The class java.util.HashMap provides similar functionality as the class java.util.EnumMap, with which you are already familiar. Refer to Lesson 9 for more information on HashMap.

[6] There were only two other collection classes: BitSit and Stack (a subclass of Vector). Both were minimally useful.

Both Vector and Hashtable are concrete; they implement no common interfaces. They incorporate support for multithreaded programming[7] by default, a choice that often results in unnecessary overhead. Unfortunately, today you will still encounter a large amount of code that uses or requires use of the Vector class.

[7] See Lesson 13 for a discussion of multithreaded programming.

Since there were no corresponding interfaces for these classes in the initial versions of Java, you were forced to assign Vector or Hashtable instances to a reference to an implementation class:

 Vector names = new Vector(); 

With the introduction of the collections framework, Sun retrofitted the Vector class to implement the List interface and the Hashtable class to implement the Map interface. You might for some reason be stuck with continuing to use a Vector or Hashtable. If so, you may be able to migrate to a modern approach by assigning the instance to an interface reference:

 List names = new Vector(); Map dictionary = new Hashtable(); 

Sun also retrofitted Vector and Hashtable so that they are parameterized types. You can thus code:

 List<String> names = new Vector<String>(); Map<Student.Grade,String> dictionary =     new Hashtable<Student.Grade,String>(); 



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net