Introduction
Nearly every program needs to store some set of data. It may be strings and numbers, objects, and so on. Arrays are a superb storage solution. But arrays have certain limitations. For example, their size is fixed, elements cannot be removed, and you can't insert elements in the middle. Collections were created to get around these and other limitations. All types of collections (and there are many of them, as we'll see later in this lesson) have the ability to dynamically resize themselves. Some collection types can store ordered elements and automatically put new elements in order as they are added.
In this lesson, we'll get acquainted with the class hierarchy of the base collections in the Java Collections Framework. There are also various alternative libraries that extend the capabilities of the standard Java Collections Framework. The most popular of these is Guava (Google Collections Library).
Basic interfaces
The diagram shows that there are two basic interfaces that are implemented to form the rest of the classes and interfaces.
Let's take a look at these interfaces:
Collection — An ordinary collection that contains a set of elements (objects). This collection has basic methods for working with elements: insert (add, addAll), remove (remove, removeAll, clear), search (contains, containsAll), check whether collection is empty (isEmpty) and get size (size).
Map — A collection structured as key-value pairs. Moreover, each key in a Map is unique: no two keys have identical values. This collection is sometimes called a dictionary. Map is a separate interface. It does not implement the Collection interface, but is part of the Java Collections Framework.
Useful methods for working with elements in a Map:
insert (put, putAll)
get (get, keySet, values, entrySet)
remove (remove, clear)
search (containsKey, containsValue)
check whether the collection is empty (isEmpty)
get size (size)
Now let's talk more about each of them.
Collection interface
The Collection interface extends the Iterable interface, which has a single method: iterator(). For us, this means that any collection that inherits Iterable will be able to return an iterator.
An iterator is a special object that you can use to access the elements of any collection, regardless of its specific implementation.
The figure shows that 3 interfaces inherit the Collection interface: List, Queue and Set. Now we'll look at each of them briefly.
List is an ordered collection that allows duplicate values. A particular feature of a List is that its elements are numbered and can be accessed by number (index).
A Queue stores elements in the order they were added to the queue.
Unlike a list, a Set represents an unordered collection that does not allow repeated elements. The Set interface corresponds to the concept of a mathematical set.
Implementations of the Map interface
We can see that the Map interface represents a mapping between unique keys and values.
interface Map<K, V>
where K is the type of the keys and V is the type of the stored values.
Using a key, we can extract data from a Map. To add an element to a Map, we must specify a key and a value.
Let's look at some implementations of Map:
HashMap is an implementation of Map that is based on hash tables. It can store keys and values of any type, including null. The order of the elements is not guaranteed.
LinkedHashMap is a data structure that stores data as a linked list of elements. The elements appear in the list in the order in which they were added.
TreeMap implements the SortedMap interface (via the NavigableMap interface). The elements in this structure are stored in sorted order (when a new element is added, the collection is sorted automatically). TreeMap is great for storing large amounts of sorted data with fast retrieval.
Outdated collections:
Java has obsolete collections from previous versions (in order to maintain backward compatibility). These old collections should not be used in new code:
Enumeration — analogous to the Iterator interface;
Vector — an ordered list of elements and analogous to the ArrayList class;
Stack — an implementation of the stack data structure, which stores and manipulates elements like how you would interact with a stack of books. There are methods for adding elements to the stack (push) and taking them off (pop);
Dictionary — analogous to the Map interface, but it is an abstract class;
Hashtable — analogous to HashMap.
You can read more about the Collections Framework in this article.
GO TO FULL VERSION