Studying up for a job interview. Big emphasis on concurrency in this company, which I’m inexperienced with. Reading Java Concurrency In Practice, and I’m finding myself reviewing the basic collection types over and over, so I’m putting them down here once and for all.
Collection: An interface representing an unordered “bag” of items, called “elements”. The “next” element is undefined (random).
Set: An interface representing a
Collectionwith no duplicates.
Setbacked by a
Hashtable. Fastest and smallest memory usage, when ordering is unimportant.
HashSetwith the addition of a linked list to associate elements in insertion order. The “next” element is the next-most-recently inserted element.
Setwhere elements are ordered by a
Comparator(typically natural ordering. Slowest and largest memory usage, but necessary for comparator-based ordering.
EnumSet: An extremely fast and efficient
Setcustomized for a single enum type.
List: An interface representing an
Collectionwhose elements are ordered and each have a numeric index representing its position, where zero is the first element, and
(length - 1)is the last.
Listbacked by an array, where the array has a length (called “capacity”) that is at least as large as the number of elements (the list’s “size”). When size exceeds capacity (when the
(capacity + 1)-thelement is added), the array is recreated with a new capacity of
(new length * 1.5)–this recreation is fast, since it uses
System.arrayCopy(). Deleting and inserting/adding elements requires all neighboring elements (to the right) be shifted into or out of that space. Accessing any element is fast, as it only requires the calculation
(element-zero-address + desired-index * element-size)to find it’s location. In most situations, an
ArrayListis preferred over a
Listbacked by a set of objects, each linked to its “previous” and “next” neighbors. A
LinkedListis also a
Deque. Accessing elements is done starting at the first or last element, and traversing until the desired index is reached. Insertion and deletion, once the desired index is reached via traversal is a trivial matter of re-mapping only the immediate-neighbor links to point to the new element or bypass the now-deleted element.
Map: An interface representing an
Collectionwhere each element has an identifying “key”–each element is a key-value pair.
Queue: An interface that represents a
Collectionwhere elements are, typically, added to one end, and removed from the other (FIFO: first-in, first-out).
Stack: An interface that represents a
Collectionwhere elements are, typically, both added (pushed) and removed (popped) from the same end (LIFO: last-in, first-out).
Deque: Pronounced “deck”. A linked list that is typically only added to and read from either end (not the middle).
Basic collection diagrams:
Comparing the insertion of an element with an
This is also an answer on Stack Overflow. Please consider up-voting it.