# Collections

![An overview of all the Collections in Java.](/files/-M6pd6fyunrXiRpc37W4)

**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**](/abstract-data-types/collections/arrays.md#array-lists) and [**Linked Lists**](/abstract-data-types/collections/linked-lists.md)**.**&#x20;
* [**Sets**](/abstract-data-types/collections/sets.md) 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](/abstract-data-types/hashing.md) 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**](/abstract-data-types/collections/stacks-and-queues.md) 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cs61b.bencuan.me/abstract-data-types/collections.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
