# Collections

![An overview of all the Collections in Java.](https://628945320-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6l_SlXhV46y96GQwmG%2F-M6pP8USz1Q9wDKjU4lh%2F-M6pd6fyunrXiRpc37W4%2Fimage.png?alt=media\&token=ef7726aa-f025-49b3-8041-7835b72e20ca)

**Collection** is a Java interface for common abstract data types that store multiple items in them.

## Sub-Interfaces

* **Lists** are indexed sequences with duplication. The two most common types are [**ArrayLists**](https://cs61b.bencuan.me/abstract-data-types/arrays#array-lists) and [**Linked Lists**](https://cs61b.bencuan.me/abstract-data-types/collections/linked-lists)**.**&#x20;
* [**Sets**](https://cs61b.bencuan.me/abstract-data-types/collections/sets) are non-indexed sequences with no duplication. (That is, every value in a set is unique.)
* **Maps** are key-value pairs. See [Hashing and Hash Tables](https://cs61b.bencuan.me/abstract-data-types/hashing) for a description on one common map implementation, the HashMap. All keys in a map must be unique, but values can be duplicated.
* [**Stacks and Queues**](https://cs61b.bencuan.me/abstract-data-types/collections/stacks-and-queues) are two ordered collections that have two core behaviors:
  * push(T x): puts x on the top.
  * pop(): Removes the first item. (See the stacks and queues page for more information.)

## Common Functions

* **Membership tests** `contains()` and `containsAll()` that can determine whether or not an element is in the collection.
* `size()` to get the number of items in the collection.
* `isEmpty()` returns true if there is nothing in the collection.
* `iterator()` returns an Iterator object to go through all the values in the collection.
* `toArray()` converts the collection to a standard Java array.
* **Optional** functions that aren't implemented in the interface: `add, addAll, clear, remove, removeAll, retainAll (intersection)`
  * Throws `UnsupportedOperationException` if not implemented.
