Java Reference
In-Depth Information
Chapter 14
The Collections Framework
WHAT YOU WILL LEARN IN THIS CHAPTER
• What sets, sequences, and maps are, and how they work
• The capabilities of the EnumSet<E> collection class
• What a Vector<T> collection object is and how to use Vector<T> objects in your programs
• How to manage Vector<T> objects so that storing and retrieving elements is typesafe
• What a Stack<T> collection is and how you use it
• How you use the LinkedList<T> collections
• How you store and retrieve objects in a hash table represented by a HashMap<K,V> object
• How you can generate hashcodes for your own class objects
In this chapter you look at the Java collections framework, which consists of generic types that represent sets
of collection classes. These types are defined in the java.util package, and they provide you with a vari-
ety of ways for structuring and managing collections of objects in your programs. In particular, the collection
types enable you to deal with situations where you don't know in advance how many objects you need to
store, or where you need a bit more flexibility in the way in which you access an object other than by the
indexing mechanism provided by an array.
UNDERSTANDING THE COLLECTIONS
FRAMEWORK
The Java collections framework is a set of generic types that you use to create collection classes that support
various ways for you to store and manage objects of any kind in memory. Recall from Chapter 13 that a col-
lection class is simply a class that organizes a set of objects of a given type in a particular way, such as in a
linked list or a pushdown stack. The majority of types that make up the collections framework are defined in
the java.util package.
Using a generic type for your collections means that you get static checking by the compiler for whatever
types of objects you want to manage. This ensures that you do not inadvertently attempt to store objects of
the wrong type in a collection. The collections framework includes a professional implementation of a gen-
eric linked list type, which is vastly superior to the linked list that you took so much trouble to develop for
yourself first as an ordinary class in Chapter 6 and later as a generic type in Chapter 13. However, the effort
wasn't entirely wasted because you now have a good idea of how linked lists work and how generic types are
defined.
Search WWH ::




Custom Search