Main Page

Previous Next

Collection Classes

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

 

Class

Description

Sets:

HashSet

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.

 

TreeSet

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 under the covers.

 

LinkedHashSet

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.

Lists:

Vector

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 Vector is the only container class that is synchronized – that is, it is well-behaved when concurrently accessed by two or more threads. We will discuss threads and synchronization in the next chapter.

 

Stack

This class is derived from Vector and adds methods to implement a stack – a last-in first-out storage mechanism.

 

LinkedList

Implements a linked list. The linked list defined by this class can also be used as a stack or a queue.

 

ArrayList

Implements an array that can vary in size and can also be accessed as a linked list. This provides a similar function to the Vector class but is unsynchronized.

Maps:

Hashtable

Implements a map where all keys must be non-null. The class defining a key must implement the hashcode() method and the equals() method to work effectively. This class is a legacy of previous Java implementations and it is usually better to use the other classes that implement maps.

 

HashMap

Implements a map that allows null objects to be stored and allows a key to be null (only one of course, since keys must be unique).

 

LinkedHashMap

Inplements a map with all of its entries in a doubly-linked list. This class can be used to create a copy of a map of any type such that the order of the entries in the copy is the same as the original.

 

WeakHashMap

Implements a map such that if a key to an object is no longer referenced ordinarily, the key/object pair will be discarded. This contrasts with HashMap where the presence of the key in the map maintains the life of the key/object pair, even though the program using the map no longer has a reference to the key, and therefore cannot retrieve the object.

 

IdentityHashMap

Implements a map using a hash table where comparisons in searching the map for a key or a value compares references, not objects. This implies that two keys are equal only if they are the same key. The same applies to values.

 

TreeMap

Implements a map such that the objects are arranged in ascending key order.

We can't go into all these classes in detail, but to introduce you to how these can be applied we will explore the three that you are likely to find most useful, Vector, LinkedList, and HashMap. Before we get into the specifics of using the container classes we need to look at the interfaces they implement, since these provide the means of applying them.

Collection Interfaces

The java.util package defines six collection interfaces that determine the methods that you use to work with each type of collection class. There are three basic collection interfaces, the Set, List, and Map interfaces, which relate to the fundamental organization of objects in a collection. These are implemented amongst the classes as follows:

Interface

Implemented by

Set

HashSet, TreeSet

List

Vector, Stack, ArrayList, LinkedList

Map

Hashtable, TreeMap, HashMap, WeakHashMap

The relationships between the interfaces that are implemented by the collection classes are shown in the following diagram.

Click To expand

The Set and List interfaces both extend a common interface, Collection. Note that the Map interface does not extend Collection. Don't confuse the Collection interface with the Collections class (with an 's') that we will see later. The two other interfaces for collections are SortedSet that extends the Set interface, and SortedMap that extends the Map interface. The TreeSet class implements the SortedSet interface, and the SortedMap interface is implemented by the TreeMap class.

It is important to keep in mind that any collection class object that implements the Collection interface can be referenced using a variable of type Collection. This means that any of the list or set collections can be referenced in this way; only the map class types are excluded (but not entirely, as you can obtain a list from a map and the classes implementing a map can provide a view of the values stored as a Collection reference). You will see that using a parameter of type Collection is a standard way of passing a list or set to a method.

These interfaces involve quite a number of methods, so rather than go through them in the abstract, let's see them at work in the context of specific classes. We will look at the Vector class first since it is close to the notion of an array that you are already familiar with.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor