Legacy classes on collection framework
1 posts in topic
Flat View  Flat View

Posted By:   Luigi_Viggiano
Posted On:   Friday, April 6, 2001 12:12 AM

There're many classes implementing algorithms to keep objects in a sort of "collection". Which is best to be used and what is better to avoid writing a new program?

Re: Legacy classes on collection framework

Posted By:   Luigi_Viggiano  
Posted On:   Friday, April 6, 2001 12:50 AM

Since Java 2, the collection framework has been redesigned, and new collections have been introduced to replace "old ones".

Now all the collection framework borns from 3 main interfaces: Map, List and Set. Collection interface is implemented by List and Set both.

Hashtable, Vector, and Stack are now called legacy classes, and have been kept to support programs written in Java 1.0 / 1.1.

The difference, from backed collections, is the data structures that physically implement your desired interface included in the new design (Map, List, and Set).

So in a new program is better to avoid use of Vector, Enumeration, Hashtable, and Stacks, as they are "Legacy Classes" and prefere to use newer following ones:

ArrayList, LinkedList (implementing List)

HashSet, TreeSet (implementing Set)

HashMap, TreeMap, WeakHashMap (implementing Map)

Is a good idea to try them in your application in the upon proposed order, an after to tune for performance.

When declaring a collection is better to use the type of the interface:

Map myMap = new HashMap();
List myList = new ArrayList();
Set mySet = new HashSet();
Collection myColl = new ArrayList();
Collection myColl2 = new HashSet();

to easyly let you to exchange implementation while tuning performances.

Cited from Thinking in Java, chapter 9 (summary):

There’s no need to use the legacy classes Vector, Hashtable and Stack in new code


Unfortunately, a lot of code was written using the Java 1.0/1.1 containers, and even new code is sometimes written using these classes. So although you should never use the old containers when writing new code, you’ll still need to be aware of them. However, the old containers were quite limited, so there’s not that much to say about them.

For further detail I suggest you to check "Thinking in Java", Chapter 9 paragraphs, Choosing an implementation and Java 1.0/1.1 containers.

About | Sitemap | Contact