14.4. Collection Classes

You have a total of 15 classes in java.util that you can use to manage collections of objects, and they support collections that are sets, lists, queues, and maps, as follows:

 ClassDescription
Sets:HashSet<T>An implementation of a set that uses HashMap<> under the covers. Although a set is by definition unordered, there has to be some way to find an object reasonably efficiently. The use of a HashMap object to implement the set enables store and retrieve operations to be done in a constant time. However, the order in which elements of the set are accessed is not necessarily constant over time.
LinkedHashSet<T>Implements a set using a hash table with all the entries linked in a doubly-linked list. This class can be used to make a copy of any set such that iteration ordering is preserved—something that does not apply to a HashSet<>.
TreeSet<T>An implementation of a set that orders the objects in the set in ascending sequence. This means that an iterator obtained from a TreeSet<> object will provide the objects in ascending sequence. The TreeSet<> classes use a TreeMap<> object under the covers.
EnumSet<T extends Enum<T>>Implements a specialized set that stores enum values from a single enum type, T
Lists:Vector<T>Implements a list as an array that automatically increases in size to accommodate as many elements as you need. Objects are stored and retrieved using an index as in a normal array. You can also use an iterator to retrieve objects from a Vector<>. The ...

Get Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.