A collection (known also as a container) is an object that groups multiple objects (elements) into a single unit.
Collections are used to store, retrieve, manipulate, and communicate aggregate data.
A collections framework is a unified architecture for representing and manipulating collections.
In Java, a collection implements the iterable interface.
The core collection interfaces form a hierarchy. A Set is a special kind of Collection, A SortedSet is a special kind of Set.
The collection interfaces are divided into two groups:
Mappings are not collections and collections are not mappings (Elements of a map elements are well known “Key-value pairs”)
We have then two tree.
All the core collection interfaces are generic.
The root of the collection hierarchy. A collection represents a group of objects known as its elements. The Collection interface is the least common denominator that all collections implement and is used to pass collections around and to manipulate them when maximum generality is desired.
Some types of collections:
The Java platform doesn't provide any direct implementations of the Collection interface but provides implementations for the following sub-interfaces:
Data Type | Allow Duplicate | Ordered |
---|---|---|
java.util.List | Yes | Yes |
java.util.Set | No | No |
java.util.SortedSet | No | Yes (Ascending) |
java.util.Map | No | No |
java.util.SortedMap | No | Yes (Ascending) |
Set is a collection that cannot contain duplicate elements.
An ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position). If you've used Vector, you're familiar with the general flavor of List.
See List
A collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations.
Queues typically, but do not necessarily, order elements in a FIFO (first-in, first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator or the elements' natural ordering.
Whatever the ordering used, the head of the queue is the element that would be removed by a call to remove or poll. In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties.
Queue - deque (Double Ended Queue)
See also the Deque Interface section.
An object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value. If you've used Hashtable, you're already familiar with the basics of Map.
SortedMap — a Map that maintains its mappings in ascending key order. This is the Map analog of SortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories.
To keep the number of core collection interfaces manageable, the Java platform doesn't provide separate interfaces for each variant of each collection type. Instead, the modification methods in each interface are designated optional. If an unsupported operation is invoked, a collection throws an UnsupportedOperationException.
Variant List:
The preferred style is to choose an implementation (interface) when a Collection is created and to immediately assign the new collection to a variable of the corresponding interface type (or to pass the collection to a method expecting an argument of the interface type). In this way, the program does not become dependent on any added methods in a given implementation, leaving the programmer free to change implementations anytime that it is warranted by performance concerns or behavioral details.
Most commonly used implementation:
Interface | Implementation |
---|---|
Set | HashSet |
List | ArrayList |
Map | HashMap |
Queue | LinkedList |
Deque | ArrayDeque |
if (myObject instanceof Collection<?>){
}
With index and all: https://github.com/npgall/cqengine
Ie: collections over primitive types
The Trove maps/sets use open addressing instead of the chaining approach taken by the JDK hashtables.