The following are several examples of collection classes:
Vector stores objects in a linear list, in order of addition. Some additional notes on Vector are:
Listwhen the collections API was developed
addElement, since the names chosen for
Collectionmethods didn't all match existing method names in
objects in a linear list, in order of addition. Methods are not synchronized, so that the class is not inherently thread-safe
(but there are now tools in the Collections API to provide a thread-safe
wrapper for any collection, which is why
Vector has fallen into disuse).
TreeSet stores objects in a linear sequence, sorted by a
comparison, with no duplicates.
Comparableitems or uses a separate
Comparatorobject to determine ordering.
Note that there is no tree list collection, because the concepts of insertion order and natural order are incompatible. Since sets reject duplicates, any comparison algorithm should include a guaranteed tiebreaker (for example, to store employees in last name, first name order: to allow for two Joe Smiths, we should include the employee id as the final level of comparison).
TreeMap stores objects in a
Map, where any subcollection or iterator obtained will be sorted by the key values. Note that:
Comparableitems or have a separate
Hashtable stores objects in a
Map. Note also that:
synchronized, so that the class is thread-safe.
Hashtablepredates the collections framework, but was marked to implement
Mapwhen the collections API was developed.
Hashtablehas some duplicate methods.
Hashtableextends an obsolete class called
HashSet uses hashing strategy to manage a
Set. Note also that:
HashSetrejects duplicate entries.
HashMapthat uses the entry
hashCodevalues as the keys.