“Any fool can know. The point is to understand.”
― Albert Einstein
Java Collections Framework is an architecture for storing, managing and manipulating collections of objects. It provides many data structures and algorithms commonly used for dealing with collections. For example: searching and sorting. Many details about storing objects are abstracted away (meaning you do not have to deal with it in code). An unified interface is provided to manipulate them e.g for adding and removing entries.
2. The Collections Hierarchy
The Collections Framework is organized into a class hierarchy which can be understood at a glance from the picture below.
Note: The hierarchy shown below includes only the most common and useful classes and interfaces. Some have been skipped to facilitate easier understanding.
Though the Map is also a part of the Java Collections Framework, it does not fall under the Collection hierarchy. Here is the Map hierarchy.
3. A Brief Introduction to Collections
- At the root of the hierarchy is Iterable which, as the name indicates, provides for iterating over the collection.
- The next is the Collection interface which provides most of the methods representing a collection. These methods include providing for adding and removing elements, checking if the collection includes an element and obtaining the number of elements in the collection.
- A Set contains no duplicate elements. Common implementations are:
- A List is an ordered sequential collection. Concrete implementations include:
- ArrayList is a re-sizable list backed by an array.
- Vector is also a re-sizable array similar to an ArrayList. Use it only when you need thread-safety and synchronization.
- Stack is a LIFO(Last-In-First-Out) array. A subclass of Vector and is also thread-safe.
- LinkedList is a doubly linked list of elements. Offers fast adds and removes from intermediate positions. Note that this class also implements the Deque interface.
- A Queue orders elements in a FIFO (First-In-First-Out) order. The add() method adds elements at the tail and remove() removes elements from the head. Typical usage is storage to hold elements before processing in the order of receipt.
- On the other hand, a Deque (Double-Ended-Queue) can add and remove elements at both ends.
- ArrayDeque is an implementation of Deque using an array for storage.
- A LinkedList is also a Deque.
4. Map and Friends
The Map interface is at the root of the hierarchy of key-value map interfaces and classes.
- Map provides the abstraction of a key-value mapping.
- Generally a Map does not provide any ordering of the entries stored in the Map. The concrete class implementing the Map is the HashMap.
- The LinkedHashMap provides a predictable iteration order of the entries in the Map.
- SortedMap extends the Map interface and provides ordering of the Map entries.
- NavigableMap adds navigation abilities to a SortedMap in the form of being able to retrieve lower-than, higher-than, etc a specified key value.
- TreeMap is the concrete implementation of the SortedMap and NavigableMap.
This article presented a brief overview of the Java Collections hierarchy. In further articles, we explore the various collection classes and their usage.
- Java Collections Introduction gives an overall picture of the most important classes and interfaces.
- Java Collections – Part 1 covers iterating over collections and addition & removal of items.
- Java Collections – Sets explains Set operations and examples for HashSet, LinkedHashSet and TreeSet.
- Java Collections – ArrayList presents ArrayList-specific API methods.