Java Collections Cheat Sheet

Java collections are a set of classes and interfaces that provide a framework for storing and manipulating groups of objects. Collections are an essential part of Java programming, and they offer a wide range of benefits, including increased efficiency, flexibility, and ease of use.

The Java collections framework includes several different types of collections, including lists, sets, maps, and queues. Each collection type has its own unique set of features and benefits, making it suitable for different use cases.

This cheat sheet provides an overview of the Java Collections Framework. It includes the most commonly used interfaces, classes, and methods, along with their descriptions and use cases.

Interfaces

InterfaceDescription
CollectionThe root interface of the collection hierarchy. It defines the basic operations that all collections should support, such as adding, removing, and iterating over elements.
ListAn ordered collection that allows duplicates. It provides positional access to elements and supports operations such as adding, removing, and searching for elements.
SetA collection that does not allow duplicates. It provides no guarantee of element order and supports operations such as adding, removing, and testing for membership.
QueueA collection that orders its elements according to a specific rule. It supports operations such as adding, removing, and peeking at the head of the queue.
DequeA double-ended queue that allows elements to be added and removed from both ends. It supports operations such as adding, removing, and peeking at the head and tail of the deque.
MapAn object that maps keys to values. It does not allow duplicate keys and provides methods for adding, removing, and accessing key-value pairs.

Classes

ClassDescription
ArrayListA resizable array implementation of the List interface. It provides constant-time positional access and is ideal for random access and iteration.
LinkedListA doubly-linked list implementation of the List interface. It provides constant-time insertion and removal at the beginning and end of the list, and is ideal for queue and stack operations.
HashSetA hash table implementation of the Set interface. It provides constant-time performance for adding, removing, and testing for membership, but does not guarantee element order.
TreeSetA red-black tree implementation of the Set interface. It provides guaranteed log(n) time performance for adding, removing, and testing for membership, and maintains elements in sorted order.
PriorityQueueA priority queue implementation of the Queue interface. It orders elements according to their natural ordering or a specified comparator, and provides constant-time performance for adding and removing the head element.
ArrayDequeAn array-based implementation of the Deque interface. It provides constant-time performance for adding and removing elements at both ends of the deque.
HashMapA hash table implementation of the Map interface. It provides constant-time performance for adding, removing, and accessing key-value pairs, but does not guarantee key order.
TreeMapA red-black tree implementation of the Map interface. It provides guaranteed log(n) time performance for adding, removing, and accessing key-value pairs, and maintains keys in sorted order.

Methods

MethodDescription
add(E e)Adds the specified element to the collection.
remove(Object o)Removes the specified element from the collection.
contains(Object o)Returns true if the collection contains the specified element.
size()Returns the number of elements in the collection.
isEmpty()Returns true if the collection contains no elements.
iterator()Returns an iterator over the elements in the collection.
clear()Removes all elements from the collection.
toArray()Returns an array containing all of the elements in the collection.
get(int index)Returns the element at the specified position in the list.
set(int index, E element)Replaces the element at the specified position in the list with the specified element.
add(int index, E element)Inserts the specified element at the specified position in the list.
remove(int index)Removes the element at the specified position in the list.
indexOf(Object o)Returns the index of the first occurrence of the specified element in the list, or -1 if the element is not found.
lastIndexOf(Object o)Returns the index of the last occurrence of the specified element in the list, or -1 if the element is not found.
containsAll(Collection<?> c)Returns true if the collection contains all of the elements in the specified collection.
addAll(Collection<? extends E> c)Adds all of the elements in the specified collection to the collection.
removeAll(Collection<?> c)Removes all of the elements in the specified collection from the collection.
retainAll(Collection<?> c)Retains only the elements in the collection that are also in the specified collection.
peek()Retrieves, but does not remove, the head of the queue.
poll()Retrieves and removes the head of the queue.
offer(E e)Inserts the specified element into the queue.
push(E e)Pushes an element onto the stack represented by the deque.
pop()Pops an element from the stack represented by the deque.
put(K key, V value)Associates the specified value with the specified key in the map.
get(Object key)Returns the value to which the specified key is mapped, or null if the map contains no mapping for the key.
remove(Object key)Removes the mapping for the specified key from the map.
containsKey(Object key)Returns true if the map contains a mapping for the specified key.
containsValue(Object value)Returns true if the map contains one or more mappings to the specified value.
keySet()Returns a set view of the keys contained in the map.
values()Returns a collection view of the values contained in the map.
entrySet()Returns a set view of the mappings contained in the map.

Reference

Java Collections Framework