Java Collections Overview

Collections are objects whose sole purpose is to store other objects, like arrays. Unlike arrays, however, collections have convenience methods to, say, return a portion of the original collection. A drawback of collections is that they can't hold primitives. (They can, however, hold wrappers, like java.lang.Integer.)

All collection objects ultimately implement the java.util.Collection interface. However, few if any implement the interface directly. There are multiple sub-interfaces of Collection that specify additional methods. These sub-interfaces decide the functionality of a collection; individual classes usually differ only in implementation. (For example, both ArrayList and LinkedList fulfill the general contract of the List interface, but do so differently.)

Most implementations of the Collection interface are in java.util. Exceptions will be noted when introduced.

Although the Map interface does not extend Collection, it is usually included in discussions of collections and will be explained here.

Hierarchy of Collections
The actual hierarchy of what extends what, and what implements what, is fairly intricate. Here is a simplified hierarchy of the collections framework:


 * Interface java.lang.Iterable
 * Interface Collection
 * Interface List
 * Class ArrayList
 * Class LinkedList (also implements Deque)
 * Class Vector
 * Class Stack (legacy class, use Deque, which is more powerful)
 * Interface Set
 * Class HashSet
 * Class LinkedHashSet
 * Interface SortedSet
 * Interface NavigableSet
 * Class TreeSet
 * Class EnumSet
 * Interface Queue
 * Class PriorityQueue
 * Interface Deque
 * Class LinkedList (also implements List)
 * Class ArrayDeque
 * Interface Map
 * Class HashMap
 * Class
 * Interface SortedMap
 * Interface NavigableMap
 * Class TreeMap

Sets
* Because an object's position within the HashSet depends on its  (and its contents), changing an object changes its position in the HashSet. Objects within a HashSet should be made immutable or should be removed, changed and then re-added to the HashSet. Otherwise mutating objects within a HashSet, will result in the  method failing to find the object.

** Since objects are added in Comparable sort order when objects are inserted and not recalculated when the object is mutated, the sort order of the TreeSet maybe be incorrect if the object is mutated. Objects within a Treeset should be made immutable or should be removed, changed and then re-added to the TreeSet.