Overview
| Type | Ordered? | Allows Duplicates? | Sorted? | Examples |
|---|---|---|---|---|
| List | Yes | Yes | No | ArrayList, LinkedList |
| Set | No | No | Yes / No | HashSet, TreeSet |
| Queue | Usually | Yes | No | LinkedList, PriorityQueue |
| Deque | Yes | Yes | No | ArrayDeque, LinkedList |
| Map | Yes (by keys) | Keys: No, Values: Yes | Yes / No | HashMap, TreeMap |
Notes
- “Ordered” means elements retain insertion order or defined order.
- “Sorted” means elements are arranged by natural order or a custom comparator.
- Maps store key-value pairs and are not part of the
Collectioninterface. - Example:
ArrayList<Object> arrayName = new ArrayList<Object>(size);
Descriptions
List
ArrayList: Dynamic array, fast random access, slower insert/removeLinkedList: Doubly linked list, fast insert/remove, slower random access
Queue / Deque (Stack)
LinkedList: Can be used as both Queue and DequePriorityQueue: Orders elements by priority (natural order or comparator)ArrayDeque: Resizable array-based deque (double-ended queue)
Concurrent Collections
BlockingQueue(interface) : For concurrency (used in producer–consumer)ArrayBlockingQueue: A bounded blocking queue backed by an array.LinkedBlockingQueue: A bounded / unbounded blocking queue backed by linked nodes.PriorityBlockingQueue: An unbounded blocking queue that orders elements based on priority.DelayQueue: An unbounded blocking queue where elements are only available after a specified delay.ConcurrentLinkedQueue: Thread-safe queue (non-blocking)CopyOnWriteArrayList: Thread-safe variant of ArrayListCopyOnWriteArraySet: Thread-safe variant of HashSetConcurrentLinkedDeque: Thread-safe dequeConcurrentSkipListMap: Sorted concurrent mapConcurrentSkipListSet: Sorted concurrent set
ArrayList
Constructors
| Constructor | Description | Example | Notes |
|---|---|---|---|
ArrayList() | Creates an empty list with an initial capacity of 10. | ArrayList<String> list = new ArrayList<>(); | Default constructor; capacity increases automatically as elements are added. |
ArrayList(int initialCapacity) | Creates an empty list with the specified initial capacity. | ArrayList<Integer> nums = new ArrayList<>(50); | Use when you know the approximate list size to reduce resizing overhead. |
ArrayList(Collection<? extends E> c) | Creates a list containing all elements of the specified collection, in iteration order. | ArrayList<String> copy = new ArrayList<>(existingList); | Copies all elements into a new list; changes to one list don’t affect the other. |
Return Type: Boolean
| Method | Description | Example | Result |
|---|---|---|---|
contains(Object o) | Checks if list contains the object. | list.contains("apple") | true |
isEmpty() | Checks if list is empty. | list.isEmpty() | false |
remove(Object o) | Removes the first occurrence of the object. | list.remove("apple") | true |
Return Type: int
| Method | Description | Example | Result |
|---|---|---|---|
size() | Returns the number of elements. | list.size() | 3 |
indexOf(Object o) | Index of first occurrence (-1 if not found). | list.indexOf("apple") | 0 |
lastIndexOf(Object o) | Index of last occurrence (-1 if not found). | list.lastIndexOf("apple") | 2 |
Return Type: E
| Method | Description | Example | Result |
|---|---|---|---|
get(int index) | Returns element at specified index. | list.get(0) | "apple" |
set(int index, E element) | Replaces element at index, returns old value. | list.set(0, "banana") | "apple" |
Return Type: void
| Method | Description | Example | Result |
|---|---|---|---|
add(E element) | Adds element to the end of the list. | list.add("orange") | — |
add(int index, E element) | Inserts element at index. | list.add(1, "kiwi") | — |
clear() | Removes all elements. | list.clear() | — |
Notes
add()/remove()requires shifting/unshifting element if it's in the middle of the array, leading to performance issues for large arrays; linked lists are preferred in those cases
LinkedList
Constructors
| Constructor | Description | Example | Notes |
|---|---|---|---|
LinkedList() | Creates an empty linked list with no initial elements. | LinkedList<String> list = new LinkedList<>(); | Default constructor; creates an empty doubly-linked list ready to add elements. |
LinkedList(Collection<? extends E> c) | Creates a linked list containing all elements of the specified collection, in iteration order. | LinkedList<Integer> copy = new LinkedList<>(existingList); | Copies all elements from another collection; maintains the same element order. |
Return Type: Boolean
| Method | Description | Example | Result |
|---|---|---|---|
contains(Object o) | Checks if the list contains the specified element. | list.contains("apple") | true |
isEmpty() | Checks if the list is empty. | list.isEmpty() | false |
remove(Object o) | Removes the first occurrence of the specified element. | list.remove("apple") | true |
Return Type: int
| Method | Description | Example | Result |
|---|---|---|---|
size() | Returns the number of elements in the list. | list.size() | 3 |
indexOf(Object o) | Returns the index of first occurrence of element (-1 if not found). | list.indexOf("apple") | 0 |
lastIndexOf(Object o) | Returns the index of last occurrence of element (-1 if not found). | list.lastIndexOf("apple") | 2 |
Return Type: E
| Method | Description | Example | Result |
|---|---|---|---|
get(int index) | Returns the element at the specified position. | list.get(0) | "apple" |
getFirst() | Returns the first element. | list.getFirst() | "apple" |
getLast() | Returns the last element. | list.getLast() | "orange" |
set(int index, E element) | Replaces element at specified index, returns old value. | list.set(0, "banana") | "apple" |
remove() | Removes and returns the first element. | list.remove() | "banana" |
removeFirst() | Removes and returns the first element. | list.removeFirst() | "apple" |
removeLast() | Removes and returns the last element. | list.removeLast() | "orange" |
Return Type: void
| Method | Description | Example | Result |
|---|---|---|---|
add(E element) | Adds element to the end of the list. | list.add("kiwi") | — |
add(int index, E element) | Inserts element at specified position. | list.add(1, "mango") | — |
addFirst(E element) | Adds element at the beginning. | list.addFirst("pear") | — |
addLast(E element) | Adds element at the end. | list.addLast("melon") | — |
clear() | Removes all elements from the list. | list.clear() | — |
HashSet
Constructors
| Constructor | Description | Example | Notes |
|---|---|---|---|
HashSet() | Creates an empty hash set with default initial capacity (16) and load factor (0.75). | HashSet<String> set = new HashSet<>(); | Default constructor; automatically resizes as elements are added. |
HashSet(Collection<? extends E> c) | Creates a hash set containing all elements from the specified collection, eliminating duplicates. | HashSet<Integer> copy = new HashSet<>(list); | Useful for removing duplicates from another collection. |
HashSet(int initialCapacity) | Creates an empty hash set with the specified initial capacity and default load factor (0.75). | HashSet<String> set = new HashSet<>(50); | Improves performance if you know approximately how many elements will be stored. |
HashSet(int initialCapacity, float loadFactor) | Creates an empty hash set with the specified initial capacity and load factor. | HashSet<String> set = new HashSet<>(100, 0.5f); | Use custom load factor to control resizing frequency; lower factor increases memory use but reduces collisions. |
Return Type: Boolean
| Method | Description | Example | Result |
|---|---|---|---|
contains(Object o) | Checks if the set contains the specified element. | set.contains("apple") | true |
remove(Object o) | Removes the specified element if present. | set.remove("apple") | true |
add(E e) | Adds the element if not already present, returns false if duplicate. | set.add("apple") | true |
isEmpty() | Checks if the set is empty. | set.isEmpty() | false |
Return Type: int
| Method | Description | Example | Result |
|---|---|---|---|
size() | Returns the number of elements in the set. | set.size() | 3 |
Return Type: void
| Method | Description | Example | Result |
|---|---|---|---|
clear() | Removes all elements from the set. | set.clear() | — |
addAll(Collection c) | Adds all elements from the specified collection. | set.addAll(otherSet) | — |
removeAll(Collection c) | Removes all elements in the specified collection. | set.removeAll(otherSet) | — |
retainAll(Collection c) | Retains only elements present in the specified collection. | set.retainAll(otherSet) | — |
HashMap
Constructors
| Constructor | Description | Example | Notes |
|---|---|---|---|
HashMap() | Creates an empty hash map with default initial capacity (16) and load factor (0.75). | HashMap<String, Integer> map = new HashMap<>(); | Default constructor; resizes automatically as entries are added. |
HashMap(int initialCapacity) | Creates an empty hash map with the specified initial capacity and default load factor (0.75). | HashMap<String, Integer> map = new HashMap<>(50); | Use this when you know approximately how many key-value pairs will be inserted to avoid rehashing. |
HashMap(int initialCapacity, float loadFactor) | Creates an empty hash map with the specified initial capacity and load factor. | HashMap<String, Integer> map = new HashMap<>(100, 0.5f); | Custom load factors control memory vs. lookup efficiency trade-offs. |
HashMap(Map<? extends K, ? extends V> m) | Creates a hash map containing all mappings from the specified map. | HashMap<String, Integer> copy = new HashMap<>(existingMap); | Copies all key-value pairs from another map; useful for cloning or conversions. |
Return Type: Boolean
| Method | Description | Example | Result |
|---|---|---|---|
containsKey(Object key) | Checks if the map contains the specified key. | map.containsKey("id") | true |
containsValue(Object value) | Checks if the map contains the specified value. | map.containsValue(42) | true |
isEmpty() | Checks if the map is empty. | map.isEmpty() | false |
remove(Object key, Object value) | Removes entry only if key maps to value. | map.remove("id", 42) | true |
Return Type: int
| Method | Description | Example | Result |
|---|---|---|---|
size() | Returns the number of key-value mappings. | map.size() | 3 |
Return Type: V
| Method | Description | Example | Result |
|---|---|---|---|
get(Object key) | Returns value mapped to key (or null if absent). | map.get("id") | 42 |
put(K key, V value) | Adds or replaces key-value mapping; returns old value or null. | map.put("id", 99) | 42 |
remove(Object key) | Removes mapping for key; returns old value or null. | map.remove("id") | 99 |
Return Type: Set / Collection
| Method | Description | Example | Result |
|---|---|---|---|
keySet() | Returns a Set view of the keys. | map.keySet() | [id, name] |
values() | Returns a Collection view of the values. | map.values() | [42, "Alice"] |
entrySet() | Returns a Set of Map.Entry objects. | map.entrySet() | [id=42, name=Alice] |
Return Type: void
| Method | Description | Example | Result |
|---|---|---|---|
clear() | Removes all mappings from the map. | map.clear() | — |
putAll(Map m) | Copies all mappings from the specified map. | map.putAll(otherMap) | — |
BlockingQueue
* BlockingQueue is an interface.
Constructors
| Constructor | Description | Example | Notes |
|---|---|---|---|
ArrayBlockingQueue(int capacity) | Creates a fixed-capacity blocking queue backed by an array. | BlockingQueue<String> q = new ArrayBlockingQueue<>(10); | Capacity must be specified; once full, producers block until space is available. |
ArrayBlockingQueue(int capacity, boolean fair) | Creates a blocking queue with optional fairness policy for access order. | BlockingQueue<String> q = new ArrayBlockingQueue<>(10, true); | Fairness = true ensures FIFO order for waiting threads (slower but predictable). |
LinkedBlockingQueue() | Creates a linked blocking queue with optional unbounded capacity (Integer.MAX_VALUE). | BlockingQueue<Integer> q = new LinkedBlockingQueue<>(); | Ideal for producer-consumer patterns; unbounded by default. |
LinkedBlockingQueue(int capacity) | Creates a linked blocking queue with the specified maximum capacity. | BlockingQueue<Integer> q = new LinkedBlockingQueue<>(100); | Use when you want to limit memory or control backpressure. |
PriorityBlockingQueue() | Creates an unbounded priority-based blocking queue with natural ordering. | BlockingQueue<Integer> q = new PriorityBlockingQueue<>(); | Uses natural ordering or a comparator for priority; not capacity-bound. |
PriorityBlockingQueue(int initialCapacity) | Creates a priority blocking queue with the specified initial capacity. | BlockingQueue<Task> q = new PriorityBlockingQueue<>(20); | Queue grows automatically beyond initial capacity as needed. |
PriorityBlockingQueue(int initialCapacity, Comparator<? super E> comparator) | Creates a priority blocking queue with custom ordering defined by a comparator. | BlockingQueue<Task> q = new PriorityBlockingQueue<>(20, comparator); | Use when you need custom sorting (e.g., by task urgency or timestamp). |
Return Type: Boolean
| Method | Description | Example | Result |
|---|---|---|---|
offer(E e) | Adds element if possible immediately, returns false if full. | queue.offer("apple") | true |
remove(Object o) | Removes a single instance of the specified element. | queue.remove("apple") | true |
contains(Object o) | Checks if the queue contains the element. | queue.contains("apple") | true |
isEmpty() | Checks if the queue is empty. | queue.isEmpty() | false |
Return Type: int
| Method | Description | Example | Result |
|---|---|---|---|
size() | Returns the number of elements currently in the queue. | queue.size() | 3 |
remainingCapacity() | Returns remaining capacity for bounded queues. | queue.remainingCapacity() | 7 |
Return Type: E
| Method | Description | Example | Result |
|---|---|---|---|
peek() | Retrieves, but does not remove, the head of the queue, or null if empty. | queue.peek() | "apple" |
poll() | Retrieves and removes the head, or null if empty. | queue.poll() | "apple" |
take() | Retrieves and removes the head, waiting if necessary until available. | queue.take() | "apple" |
element() | Retrieves, but does not remove, the head. Throws exception if empty. | queue.element() | "apple" |
remove() | Retrieves and removes the head. Throws exception if empty. | queue.remove() | "apple" |
Return Type: void
| Method | Description | Example | Result |
|---|---|---|---|
put(E e) | Adds element, waiting if necessary for space in bounded queue. | queue.put("apple") | — |