Java Collection Framework β Overview Table
| Class | Interface(s) Implemented | Also Implements / Extends | Ordering | Duplicates | Null Allowed | Thread Safe | FailβFast Iterator | Performance (Avg) | Legacy/Modern | Typical Use Case |
|---|
ArrayList π΄ | List | RandomAccess, Cloneable, Serializable | Insertion | β
Yes | β
Multiple nulls | β No | β
Yes | O(1) get, O(n) remove | Modern | Dynamic array, fast random access |
LinkedList π’ | List, Deque, Queue | Cloneable, Serializable | Insertion | β
Yes | β
Yes | β No | β
Yes | O(1) add/remove at ends | Modern | Frequent insertion/deletion or queue |
Vector π΄π΄π | List | RandomAccess, Cloneable, Serializable | Insertion | β
Yes | β
Yes | β
Yes | β
Yes | O(1) synchronized | Legacy | Thread-safe list (historical use) |
Stack π΄π΄π | List (via Vector) | extends Vector | LIFO | β
Yes | β
Yes | β
Yes | β
Yes | O(1) push/pop | Legacy | Classic stack operations |
CopyOnWriteArrayList π΄π | List | Cloneable, Serializable | Insertion | β
Yes | β
Yes | β
Yes | β (snapshot) | O(n) writes, O(1) read | Modern (Concurrent) | Thread-safe mostly-read list |
HashSet π΅ | Set | Cloneable, Serializable | Unordered | β No | β
One null | β No | β
Yes | O(1) operations | Modern | Unique items, high performance |
LinkedHashSet π΅π’ | Set | extends HashSet, Serializable | Insertion | β No | β
Yes | β No | β
Yes | O(1) operations | Modern | Maintains insertion order |
TreeSet π£ | NavigableSet, SortedSet | Cloneable, Serializable | Sorted | β No | β No | β No | β
Yes | O(log n) operations | Modern | Sorted set, ordered operations |
CopyOnWriteArraySet π΄π | Set | Cloneable, Serializable | Insertion | β No | β
Yes | β
Yes | β (snapshot) | O(n) writes, O(1) read | Modern (Concurrent) | Read-heavy thread-safe set |
ConcurrentSkipListSet π£π | NavigableSet, SortedSet, Set | Cloneable, Serializable | Sorted | β No | β No | β
Yes | β (weakly consistent) | O(log n) concurrent ops | Modern (Concurrent) | Highly concurrent sorted set |
PriorityQueue π‘ | Queue | Serializable | Priority (custom/natural) | β
Yes | β
Yes | β No | β
Yes | O(log n) insertion | Modern | Priority-based ordering |
ArrayDeque π΄ | Deque | Cloneable, Serializable | Insertion | β
Yes | β No | β No | β
Yes | O(1) at ends | Modern | Efficient stack/queue without blocking |
LinkedBlockingDeque π’π | BlockingDeque, Deque, Queue | Serializable | FIFO or LIFO | β
Yes | β
Yes | β
Yes | β (weakly consistent) | O(1) operations | Modern (Concurrent) | Thread-safe double-ended queue |
LinkedBlockingQueue π’π | BlockingQueue, Queue | Serializable | FIFO | β
Yes | β
Yes | β
Yes | β | O(1) operations | Modern (Concurrent) | Producer-consumer queue |
ArrayBlockingQueue π΄π | BlockingQueue, Queue | Serializable | FIFO (bounded) | β
Yes | β No | β
Yes | β | O(1) operations | Modern (Concurrent) | Fixed-size blocking queue |
PriorityBlockingQueue π‘π | BlockingQueue, Queue | Serializable | Priority | β
Yes | β
Yes | β
Yes | β | O(log n) insertion | Modern (Concurrent) | Concurrent priority queue |
DelayQueue π‘π | BlockingQueue, Queue | Serializable | Delay-ordered | β
Yes | β No | β
Yes | β | O(log n) operations | Modern (Concurrent) | Scheduled task queue |
SynchronousQueue βͺπ | BlockingQueue, Queue | Serializable | None (hand-off only) | β No | β No | β
Yes | β | O(1) | Modern (Concurrent) | Thread rendezvous queue |
LinkedTransferQueue π’π | TransferQueue, BlockingQueue, Queue | Serializable | FIFO | β
Yes | β
Yes | β
Yes | β | O(1) operations | Modern (Concurrent) | High-throughput producer-consumer handoff |
HashMap π΅ | Map | Cloneable, Serializable | Unordered | β Keys | β
one null key, multiple null values | β No | β
Yes | O(1) operations | Modern | General-purpose map |
LinkedHashMap π’π΅ | Map | extends HashMap, Serializable | Insertion | β No | β
Yes | β No | β
Yes | O(1) operations | Modern | Maintains insertion order |
TreeMap π£ | NavigableMap, SortedMap, Map | Cloneable, Serializable | Sorted | β No | β No | β No | β
Yes | O(log n) operations | Modern | Sorted key-value map |
ConcurrentHashMap π΅π | ConcurrentMap, Map | Serializable, Cloneable | Unordered | β No | β No | β
Yes | β (weakly consistent) | O(1) concurrent access | Modern (Concurrent) | High-performance concurrent map |
ConcurrentSkipListMap π£π | ConcurrentNavigableMap, Map | Serializable, Cloneable | Sorted | β No | β No | β
Yes | β (weakly consistent) | O(log n) concurrent sorted ops | Modern (Concurrent) | Concurrent sorted map |
WeakHashMap π΅ | Map | Serializable, Cloneable | Unordered (GC-based) | β No | β
Yes | β No | β
Yes | O(1) (GC-sensitive) | Modern | Auto-removable keys on weak reachability |
IdentityHashMap π΅ | Map | Serializable, Cloneable | Unordered | β No | β
Yes | β No | β
Yes | O(1) operations | Modern | Reference equality-based map |
EnumMap π΄ | Map | Serializable, Cloneable | Enum key order | β No | β null keys | β No | β
Yes | O(1) operations | Modern | Efficient enum-key-based map |
Hashtable π΅π΄π | Map | Cloneable, Serializable | Unordered | β No | β No | β
Yes | β
Yes | O(1) synchronized | Legacy | Legacy thread-safe map |
Dictionary π΄ (abstract) | none (abstract legacy) | β | Unordered | β No | β No | β No | β | Varies | Legacy | PreβMap abstract key/value class |
π΄Array β π’Linked List β π£Tree β π΅Hash Table β π‘Binary Heap β πThread-safe β π΄Legacy
Java Collection Framework β Complete Method Reference
Collection Interface
| Method Name | Arguments | Return Type | Description | Example |
|---|
add | _E e_ (required) | boolean | Adds element to the collection | collection.add("item"); |
addAll | _Collection<? extends E> c_ (required) | boolean | Adds all elements from another collection | collection.addAll(otherCollection); |
clear | Β | void | Removes all elements | collection.clear(); |
contains | _Object o_ (required) | boolean | Checks if collection contains the element | collection.contains("item"); |
containsAll | _Collection<?> c_ (required) | boolean | Checks if collection contains all elements in c | collection.containsAll(otherCollection); |
equals | _Object o_ (required) | boolean | Checks equality with another object | collection.equals(anotherCollection); |
hashCode | Β | int | Returns hash code | collection.hashCode(); |
isEmpty | Β | boolean | Checks if collection is empty | collection.isEmpty(); |
iterator | Β | Iterator<E> | Returns an iterator over elements | Iterator<E> it = collection.iterator(); |
parallelStream | Β | Stream<E> | Returns a parallel Stream of elements | collection.parallelStream().forEach(...); |
remove | _Object o_ (required) | boolean | Removes a single instance of specified element | collection.remove("item"); |
removeAll | _Collection<?> c_ (required) | boolean | Removes all elements contained in collection c | collection.removeAll(otherCollection); |
removeIf | _Predicate<? super E> filter_ (required) | boolean | Removes all elements matching predicate | collection.removeIf(e -> e.isEmpty()); |
retainAll | _Collection<?> c_ (required) | boolean | Retains only elements contained in collection c | collection.retainAll(otherCollection); |
size | Β | int | Returns number of elements | int size = collection.size(); |
stream | Β | Stream<E> | Returns a sequential Stream of elements | collection.stream().filter(...); |
toArray | Β | Object[] | Returns an array of all elements | Object[] arr = collection.toArray(); |
toArray | _T[] a_ (required) | T[] | Returns array containing elements of runtime type | String[] arr = collection.toArray(new String[0]); |
forEach | _Consumer<? super E> action_ (required) | void | Performs given action for each element | collection.forEach(System.out::println); |
spliterator | Β | Spliterator<E> | Returns a Spliterator over elements | Spliterator<E> spliterator = collection.spliterator(); |
List<E> Interface β Full Cheatsheet
| Method Name | Arguments | Return Type | Description | Avg. Time β± | Java Version | Example |
|---|
addβ
β
β
| int index, E element (required) | void | Inserts element at specified position | O(n) | 1.2 | list.add(1, "item"); |
addβ
β
β
| E element (required) | boolean | Adds element to end of list | O(1) amort. | 1.2 | list.add("item"); |
addAllβ
β
| int index, Collection<? extends E> c | boolean | Inserts all elements at specified position | O(n + m) | 1.2 | list.addAll(1, collection); |
addAllβ
β
β
| Collection<? extends E> c | boolean | Adds all elements to end of list | O(m) | 1.2 | list.addAll(collection); |
clearβ
β
β
| Β | void | Removes all elements | O(n) | 1.2 | list.clear(); |
containsβ
β
β
| Object o (required) | boolean | Checks if element is present | O(n) | 1.2 | list.contains("item"); |
containsAllβ
β
| Collection<?> c (required) | boolean | Checks if all elements are present | O(nΓm) | 1.2 | list.containsAll(collection); |
equalsβ
β
| Object o (required) | boolean | Compares equality | O(n) | 1.2 | list.equals(otherList); |
getβ
β
β
| int index (required) | E | Returns element at position | O(1) | 1.2 | E element = list.get(1); |
indexOfβ
β
β
| Object o (required) | int | Returns index of first occurrence | O(n) | 1.2 | int i = list.indexOf("item"); |
isEmptyβ
β
β
| Β | boolean | Checks if list is empty | O(1) | 1.2 | list.isEmpty(); |
iteratorβ
β
| Β | Iterator<E> | Returns iterator | O(1) | 1.2 | Iterator<E> it = list.iterator(); |
lastIndexOfβ
β
| Object o (required) | int | Returns index of last occurrence | O(n) | 1.2 | int i = list.lastIndexOf("item"); |
listIteratorβ
β
| Β | ListIterator<E> | Returns list iterator | O(1) | 1.2 | ListIterator<E> it = list.listIterator(); |
listIteratorβ
β
| int index (required) | ListIterator<E> | Returns list iterator starting at index | O(1) | 1.2 | ListIterator<E> it = list.listIterator(1); |
removeβ
β
β
| int index (required) | E | Removes element at index | O(n) | 1.2 | E removed = list.remove(1); |
removeβ
β
β
| Object o (required) | boolean | Removes first occurrence of element | O(n) | 1.2 | list.remove("item"); |
removeAllβ
β
| Collection<?> c (required) | boolean | Removes all matching elements | O(nΓm) | 1.2 | list.removeAll(collection); |
retainAllβ
β
| Collection<?> c (required) | boolean | Retains only elements present in collection | O(nΓm) | 1.2 | list.retainAll(collection); |
setβ
β
β
| int index, E element (required) | E | Replaces element at index | O(1) | 1.2 | list.set(1, "newItem"); |
sizeβ
β
β
| Β | int | Returns size of list | O(1) | 1.2 | int size = list.size(); |
subListβ
β
| int fromIndex, int toIndex (required) | List<E> | Returns sublist view | O(1)ΒΉ | 1.2 | List<E> sub = list.subList(1, 3); |
toArrayβ
β
β
| Β | Object[] | Returns array of elements | O(n) | 1.2 | Object[] arr = list.toArray(); |
toArrayβ
β
β
| T[] a (required) | <T[]> | Returns array in provided array type | O(n) | 1.2 | String[] arr = list.toArray(new String[0]); |
ποΈ Static Methods in List Interface
| Method Name | Arguments | Return Type | Description | Avg. Time β± | Java Version | Example |
|---|
ofβ
β
β
| E... elements (required) | List<E> | Immutable list of elements | O(n) | 9+ | List.of("a", "b") |
copyOfβ
β
| Collection<? extends E> c (required) | List<E> | Immutable copy of collection | O(n) | 9+ | List.copyOf(myList) |
π οΈ Utility Methods from Collections and Arrays
| Method Name | Arguments | Return Type | Description | Avg. Time β± | Java Version | Example |
|---|
Collections.unmodifiableListβ
β
| List<? extends T> list (required) | List<T> | Read-only wrapper over original list | O(1) | 1.2 | Collections.unmodifiableList(myList) |
Collections.singletonListβ
β
| T item (required) | List<T> | Immutable list with a single element | O(1) | 1.2 | Collections.singletonList("a") |
Arrays.asListβ
β
β
| T... a (required) | List<T> | Fixed-size list backed by an array | O(1) | 1.2 | Arrays.asList("x", "y") |
Queue<E> Interface β Full Cheatsheet
| Method Name | Arguments | Return Type | Description | Avg. Time β± | Java Version | Example |
|---|
addβ
β
β
| E e (required) | boolean | Inserts element or throws exception | O(1) | 1.5 | queue.add("item"); |
offerβ
β
β
| E e (required) | boolean | Inserts element or returns false if full | O(1) | 1.5 | queue.offer("item"); |
pollβ
β
β
| Β | E | Retrieves and removes head, or null if empty | O(1) | 1.5 | E e = queue.poll(); |
removeβ
β
| Β | E | Retrieves and removes head, throws if empty | O(1) | 1.5 | E e = queue.remove(); |
peekβ
β
β
| Β | E | Retrieves head or null if empty | O(1) | 1.5 | E e = queue.peek(); |
elementβ
β
| Β | E | Retrieves head or throws if empty | O(1) | 1.5 | E e = queue.element(); |
ποΈ Static Methods in Collections (Queue Utilities)
| Method Name | Arguments | Return Type | Description | Avg. Time β± | Java Version | Example |
|---|
Collections.asLifoQueueβ
| Deque<T> deque (required) | Queue<T> | Returns a LIFO view of a deque | O(1) | 1.6 | Queue<T> lifo = Collections.asLifoQueue(deque); |
Collections.unmodifiableQueueβ
| Queue<? extends T> queue (required) | Queue<T> | Returns an unmodifiable view of a queue | O(1) | 1.5 | Queue<T> unmod = Collections.unmodifiableQueue(queue); |
π οΈ Static Factory Methods (Java 9+)
| Method Name | Arguments | Return Type | Description | Avg. Time β± | Java Version | Example |
|---|
Queue.ofβ
β
β
| E... elements (required) | Queue<E> | Creates an unmodifiable queue with elements | O(n) | 9+ | Queue<String> q = Queue.of("A", "B"); |
Queue.copyOfβ
β
| Collection<? extends E> coll (required) | Queue<E> | Creates an unmodifiable queue from collection | O(n) | 9+ | Queue<String> q = Queue.copyOf(list); |
Deque Interface (extends Queue)
| Method Name | Arguments | Return Type | Description | Example |
|---|
addFirst | _E e_ (required) | void | Inserts at front | deque.addFirst("item"); |
addLast | _E e_ (required) | void | Inserts at end | deque.addLast("item"); |
offerFirst | _E e_ (required) | boolean | Inserts at front or false if full | deque.offerFirst("item"); |
offerLast | _E e_ (required) | boolean | Inserts at end or false if full | deque.offerLast("item"); |
pollFirst | Β | E | Retrieves and removes first or null if empty | E e = deque.pollFirst(); |
pollLast | Β | E | Retrieves and removes last or null if empty | E e = deque.pollLast(); |
peekFirst | Β | E | Retrieves first or null if empty | E e = deque.peekFirst(); |
peekLast | Β | E | Retrieves last or null if empty | E e = deque.peekLast(); |
removeFirst | Β | E | Retrieves and removes first, throws if empty | E e = deque.removeFirst(); |
removeLast | Β | E | Retrieves and removes last, throws if empty | E e = deque.removeLast(); |
getFirst | Β | E | Retrieves first, throws if empty | E e = deque.getFirst(); |
getLast | Β | E | Retrieves last, throws if empty | E e = deque.getLast(); |
removeFirstOccurrence | _Object o_ (required) | boolean | Removes first occurrence of object | deque.removeFirstOccurrence(obj); |
removeLastOccurrence | _Object o_ (required) | boolean | Removes last occurrence of object | deque.removeLastOccurrence(obj); |
Map<K,V> Interface
| Method Name | Arguments | Return Type | Description | Example |
|---|
clear | Β | void | Removes all mappings | map.clear(); |
compute | _K key_, _BiFunction<? super K, ? super V, ? extends V> remappingFunction_ (required) | V | Computes new value | map.compute("key", (k,v) -> v == null ? 1 : v + 1); |
computeIfAbsent | _K key_, _Function<? super K, ? extends V> mappingFunction_ (required) | V | Computes value if absent | map.computeIfAbsent("key", k -> 1); |
computeIfPresent | _K key_, _BiFunction<? super K, ? super V, ? extends V> remappingFunction_ (required) | V | Computes new value if present | map.computeIfPresent("key", (k,v) -> v + 1); |
containsKey | _Object key_ (required) | boolean | Checks if map contains key | map.containsKey("key"); |
containsValue | _Object value_ (required) | boolean | Checks if map contains value | map.containsValue("value"); |
entrySet | Β | Set<Map.Entry<K,V>> | Returns a set view of mappings | Set<Map.Entry<K,V>> entries = map.entrySet(); |
equals | _Object o_ (required) | boolean | Checks equality | map.equals(otherMap); |
forEach | _BiConsumer<? super K, ? super V> action_ (required) | void | Performs action for each mapping | map.forEach((k,v) -> System.out.println(k + v)); |
get | _Object key_ (required) | V | Returns value for key | V val = map.get("key"); |
getOrDefault | _Object key_, _V defaultValue_ (required) | V | Returns value or default if absent | V val = map.getOrDefault("key", defaultVal); |
isEmpty | Β | boolean | Checks if map is empty | map.isEmpty(); |
keySet | Β | Set<K> | Returns set of keys | Set<K> keys = map.keySet(); |
merge | _K key_, _V value_, _BiFunction<? super V, ? super V, ? extends V> remappingFunction_ (required) | V | Merges value | map.merge("key", "value", (v1, v2) -> v1 + v2); |
put | _K key_, _V value_ (required) | V | Adds or replaces mapping | map.put("key", "value"); |
putAll | _Map<? extends K, ? extends V> m_ (required) | void | Copies all mappings | map.putAll(otherMap); |
remove | _Object key_ (required) | V | Removes mapping | map.remove("key"); |
replace | _K key_, _V value_ (required) | V | Replaces value for key | map.replace("key", "newValue"); |
replaceAll | _BiFunction<? super K, ? super V, ? extends V> function_ (required) | void | Replaces all values using function | map.replaceAll((k, v) -> v.toUpperCase()); |
size | Β | int | Returns number of mappings | int size = map.size(); |
values | Β | Collection<V> | Returns collection of values | Collection<V> values = map.values(); |
SortedMap<K,V> Interface (extends Map<K,V>)
| Method Name | Arguments | Return Type | Description | Example |
|---|
comparator | Β | Comparator<? super K> | Returns comparator or null if natural | Comparator<K> c = sortedMap.comparator(); |
subMap | _K fromKey_, _K toKey_ (required) | SortedMap<K,V> | Returns view between keys | SortedMap<K,V> sm = sortedMap.subMap(fromKey, toKey); |
headMap | _K toKey_ (required) | SortedMap<K,V> | Returns view of keys less than toKey | SortedMap<K,V> hm = sortedMap.headMap(toKey); |
tailMap | _K fromKey_ (required) | SortedMap<K,V> | Returns view of keys greater or equal | SortedMap<K,V> tm = sortedMap.tailMap(fromKey); |
firstKey | Β | K | Returns first (lowest) key | K first = sortedMap.firstKey(); |
lastKey | Β | K | Returns last (highest) key | K last = sortedMap.lastKey(); |
NavigableMap<K,V> Interface (extends SortedMap<K,V>)
| Method Name | Arguments | Return Type | Description | Example |
|---|
ceilingEntry | _K key_ (required) | Map.Entry<K,V> | Least entry with key β₯ given key | Map.Entry<K,V> e = navMap.ceilingEntry(key); |
ceilingKey | _K key_ (required) | K | Least key β₯ given key | K k = navMap.ceilingKey(key); |
descendingKeySet | Β | NavigableSet<K> | Returns reverse order key set | NavigableSet<K> ks = navMap.descendingKeySet(); |
descendingMap | Β | NavigableMap<K,V> | Returns reverse order map | NavigableMap<K,V> dm = navMap.descendingMap(); |
floorEntry | _K key_ (required) | Map.Entry<K,V> | Greatest entry with key β€ given key | Map.Entry<K,V> e = navMap.floorEntry(key); |
floorKey | _K key_ (required) | K | Greatest key β€ given key | K k = navMap.floorKey(key); |
higherEntry | _K key_ (required) | Map.Entry<K,V> | Least entry with key > given key | Map.Entry<K,V> e = navMap.higherEntry(key); |
higherKey | _K key_ (required) | K | Least key > given key | K k = navMap.higherKey(key); |
headMap | _K toKey_, _boolean inclusive_ (required) | NavigableMap<K,V> | Returns view of keys less than (inclusive) | NavigableMap<K,V> hm = navMap.headMap(toKey, true); |
lastEntry | Β | Map.Entry<K,V> | Returns last entry | Map.Entry<K,V> e = navMap.lastEntry(); |
lowerEntry | _K key_ (required) | Map.Entry<K,V> | Greatest entry with key < given key | Map.Entry<K,V> e = navMap.lowerEntry(key); |
lowerKey | _K key_ (required) | K | Greatest key < given key | K k = navMap.lowerKey(key); |
pollFirstEntry | Β | Map.Entry<K,V> | Removes and returns first entry | Map.Entry<K,V> e = navMap.pollFirstEntry(); |
pollLastEntry | Β | Map.Entry<K,V> | Removes and returns last entry | Map.Entry<K,V> e = navMap.pollLastEntry(); |
subMap | _K fromKey_, _boolean fromInclusive_, _K toKey_, _boolean toInclusive_ (required) | NavigableMap<K,V> | Returns view between keys | NavigableMap<K,V> sm = navMap.subMap(fromKey, true, toKey, false); |
tailMap | _K fromKey_, _boolean inclusive_ (required) | NavigableMap<K,V> | Returns view of keys β₯ given key | NavigableMap<K,V> tm = navMap.tailMap(fromKey, true); |
SortedSet Interface
| Method Name | Arguments | Return Type | Description | Example |
|---|
comparator | Β | Comparator<? super E> | Returns the comparator used for sorting, or null if natural order | Comparator c = sortedSet.comparator(); |
first | Β | E | Returns the first (lowest) element | E first = sortedSet.first(); |
last | Β | E | Returns the last (highest) element | E last = sortedSet.last(); |
headSet | _E toElement_ (required) | SortedSet<E> | Elements strictly less than toElement | SortedSet<E> head = sortedSet.headSet(e); |
tailSet | _E fromElement_ (required) | SortedSet<E> | Elements greater than or equal to fromElement | SortedSet<E> tail = sortedSet.tailSet(e); |
subSet | _E fromElement_, _E toElement_ (required) | SortedSet<E> | Elements in range from (inclusive) to to (exclusive) | SortedSet<E> sub = sortedSet.subSet(from, to); |
NavigableSet Interface
| Method Name | Arguments | Return Type | Description | Example |
|---|
ceiling | _E e_ (required) | E | Returns the least element β₯ e, or null if none | E c = navSet.ceiling(e); |
floor | _E e_ (required) | E | Returns the greatest element β€ e, or null if none | E f = navSet.floor(e); |
higher | _E e_ (required) | E | Returns the least element > e, or null if none | E h = navSet.higher(e); |
lower | _E e_ (required) | E | Returns the greatest element < e, or null if none | E l = navSet.lower(e); |
pollFirst | Β | E | Retrieves and removes the first (lowest) element, or null | E first = navSet.pollFirst(); |
pollLast | Β | E | Retrieves and removes the last (highest) element, or null | E last = navSet.pollLast(); |
descendingSet | Β | NavigableSet<E> | Returns a reverse-order view of the set | NavigableSet<E> rev = navSet.descendingSet(); |
descendingIterator | Β | Iterator<E> | Returns an iterator over elements in reverse order | Iterator<E> it = navSet.descendingIterator(); |
headSet | _E toElement_, _boolean inclusive_ (required) | NavigableSet<E> | View of elements less than (or equal) to toElement | NavigableSet<E> hs = navSet.headSet(e, true); |
tailSet | _E fromElement_, _boolean inclusive_ (required) | NavigableSet<E> | View of elements greater than (or equal) to fromElement | NavigableSet<E> ts = navSet.tailSet(e, true); |
subSet | _E fromElement_, _boolean fromInclusive_, _E toElement_, _boolean toInclusive_ (required) | NavigableSet<E> | View of elements between fromElement and toElement | NavigableSet<E> ss = navSet.subSet(f, true, t, false); |
ConcurrentMap<K,V> Interface
| Method Name | Arguments | Return Type | Description | Example |
|---|
putIfAbsent | _K key_, _V value_ (required) | V | If key not present, associates value with key atomically; returns previous value or null if none | V oldVal = cmap.putIfAbsent(key, value); |
remove | _Object key_, _Object value_ (required) | boolean | Removes entry only if key is mapped to given value | boolean removed = cmap.remove(key, value); |
replace | _K key_, _V oldValue_, _V newValue_ (required) | boolean | Replaces entry for key only if currently mapped to oldValue | boolean replaced = cmap.replace(key, oldVal, newVal); |
replace | _K key_, _V value_ (required) | V | Replaces entry for key only if present; returns previous value or null if none | V oldVal = cmap.replace(key, value); |
computeIfAbsent | _K key_, _Function<? super K,? extends V> mappingFunction_ (required) | V | If key absent, computes value using mappingFunction and inserts it atomically | V val = cmap.computeIfAbsent(key, k -> newVal); |
computeIfPresent | _K key_, _BiFunction<? super K,? super V,? extends V> remappingFunction_ (required) | V | If key present, computes new value using remappingFunction; replaces entry atomically | V val = cmap.computeIfPresent(key, (k,v) -> newVal); |
compute | _K key_, _BiFunction<? super K,? super V,? extends V> remappingFunction_ (required) | V | Computes new mapping for key atomically | V val = cmap.compute(key, (k,v) -> newVal); |
merge | _K key_, _V value_, _BiFunction<? super V,? super V,? extends V> remappingFunction_ (required) | V | Merges existing value with given value using remappingFunction atomically | V val = cmap.merge(key, value, (v1,v2) -> mergedVal); |
BlockingQueue Interface
| Method Name | Arguments | Return Type | Description | Example |
|---|
put | E e (required) | void | Inserts the element, waiting if necessary for space to become available | queue.put(e); |
take | none | E | Retrieves and removes the head, waiting if necessary until an element becomes available | E e = queue.take(); |
offer | E e, long timeout, TimeUnit unit (required) | boolean | Inserts the element, waiting up to the timeout if necessary for space | boolean success = queue.offer(e, 1, TimeUnit.SECONDS); |
poll | long timeout, TimeUnit unit (required) | E | Retrieves and removes the head, waiting up to the timeout if necessary | E e = queue.poll(1, TimeUnit.SECONDS); |
remainingCapacity | none | int | Returns the number of additional elements the queue can ideally accept | int cap = queue.remainingCapacity(); |
drainTo | Collection<? super E> c (required) | int | Removes all available elements and adds them to the given collection | int drained = queue.drainTo(list); |
drainTo | Collection<? super E> c, int maxElements (required) | int | Removes up to maxElements and adds them to the given collection | int drained = queue.drainTo(list, 10); |
TransferQueue Interface
| Method Name | Arguments | Return Type | Description | Example |
|---|
transfer | E e (required) | void | Inserts the element and waits until it is received by a consumer | queue.transfer(e); |
tryTransfer | E e (required) | boolean | Attempts to transfer the element immediately, returns false if no consumer is waiting | boolean success = queue.tryTransfer(e); |
tryTransfer | E e, long timeout, TimeUnit unit (required) | boolean | Waits up to timeout for a consumer to receive the element; returns false if timed out | boolean success = queue.tryTransfer(e, 1, TimeUnit.SECONDS); |
hasWaitingConsumer | none | boolean | Returns true if any consumers are waiting to receive elements | boolean waiting = queue.hasWaitingConsumer(); |
getWaitingConsumerCount | none | int | Returns an estimate of the number of consumers waiting to receive elements | int count = queue.getWaitingConsumerCount(); |
ConcurrentNavigableMap<K,V> Interface
| Method Name | Arguments | Return Type | Description | Example |
|---|
subMap | K fromKey, boolean fromInclusive, K toKey, boolean toInclusive | ConcurrentNavigableMap<K,V> | Returns a view of the portion of this map whose keys range from fromKey to toKey | ConcurrentNavigableMap<K,V> view = map.subMap(k1, true, k2, false); |
headMap | K toKey, boolean inclusive | ConcurrentNavigableMap<K,V> | Returns a view of the portion of this map whose keys are less than (or equal if inclusive) | ConcurrentNavigableMap<K,V> head = map.headMap(k, true); |
tailMap | K fromKey, boolean inclusive | ConcurrentNavigableMap<K,V> | Returns a view of the portion of this map whose keys are greater than (or equal if inclusive) | ConcurrentNavigableMap<K,V> tail = map.tailMap(k, false); |
descendingMap | none | ConcurrentNavigableMap<K,V> | Returns a reverse-order view of the map | ConcurrentNavigableMap<K,V> rev = map.descendingMap(); |
Argument Types Description
β‘οΈ _E e_:
Represents a single element of generic type E. Used in methods that add, remove, or process one element.
β‘οΈ _Collection<? extends E> c_:
A collection containing elements of type E or its subtypes. Used when adding or manipulating multiple elements at once.
β‘οΈ _Collection<?> c_:
A collection with unknown element type, commonly used for removal or retention operations.
β‘οΈ _Object o_:
A general object parameter, used in equality checks or removal of a specific element.
β‘οΈ _Predicate<? super E> filter_:
A functional interface that tests elements for a condition, used to filter or remove elements matching the predicate.
β‘οΈ _Consumer<? super E> action_:
A functional interface representing an action to perform on each element (e.g., printing, modifying).
β‘οΈ _T[] a_:
A generic array provided to toArray to specify the output arrayβs runtime type.
β‘οΈ _int index_:
A zero-based integer index specifying position in the list for insertion, retrieval, or removal.
β‘οΈ _int fromIndex_:
A starting index, inclusive, for sublist or subrange operations.
β‘οΈ _int toIndex_:
An ending index, exclusive, for sublist or subrange operations.
β‘οΈ _K key_:
The key object used in map operations such as lookup, insertion, or deletion.
β‘οΈ _V value_:
The value associated with a key in map operations, used for insertion or replacement.
β‘οΈ _Map<? extends K, ? extends V> m_:
A map of key-value pairs, used to bulk insert into another map.
β‘οΈ _boolean inclusive_:
A boolean flag indicating whether boundary keys should be included (true) or excluded (false) in range operations.
β‘οΈ _boolean fromInclusive_, _boolean toInclusive_:
Boolean flags specifying inclusiveness of the start and end keys in subrange views or operations.
β‘οΈ _Comparator<? super E> comparator_:
A comparator defining the order of elements in sorted collections or maps.
β‘οΈ _Iterable<? extends E> iterable_:
An iterable collection of elements used as input for bulk operations.
β‘οΈ _Spliterator<E> spliterator_:
An object supporting parallel traversal over elements.
β‘οΈ No arguments:
Methods with no parameters typically perform queries, clear, or return iterators over the whole collection.
Marker & Utility Interfaces
Marker & Utility Interfaces in Java β In-Depth Comparison
| Interface | Package | Type | Purpose | Declared Methods | Common Implementations | Key Notes |
|---|
RandomAccess | java.util | Marker Interface | Indicates that a List implementation supports fast random access (e.g., O(1) get/index) | β None | ArrayList, Vector | Used by algorithms to decide if index-based traversal is efficient; not used by LinkedList |
Cloneable | java.lang | Marker Interface | Signals that .clone() method can be called safely; enables field-by-field memory copy | β None | ArrayList, HashMap, Date | Must override clone() manually from Object; deep vs shallow copy must be handled explicitly |
Serializable | java.io | Marker Interface | Marks a class as eligible for Java Serialization β converting object state to a byte stream | β None | ArrayList, HashMap, String | Used in I/O, RMI, caching; fields not marked transient will be persisted |
Externalizable | java.io | Functional Interface | Like Serializable, but gives full control over what/how to serialize/deserialize | β
writeExternal, readExternal | Rarely used directly | Must define a public no-arg constructor; improves performance & control |
Comparable<T> | java.lang | Generic Interface | Enables natural ordering of objects (e.g., alphabetic or numeric sorting) via compareTo() | β
compareTo(T o) | String, Integer, Date | Needed for TreeSet, TreeMap; should be consistent with equals() |
Comparator<T> | java.util | Generic Interface | Enables external/custom ordering logic without modifying the class | β
compare(T o1, T o2) | Used with Collections.sort() | Can create multiple sort orders; supports chaining with thenComparing() in Java 8+ |
Summary by Behavior
| Interface | Is Marker? | Serialization | Cloning | Sorting | Access Optimization | Control Level | Usage Context |
|---|
RandomAccess | β
| β | β | β | β
(O(1) access hint) | π« No control | Algorithm optimization hints |
Cloneable | β
| β | β
| β | β | β οΈ Partial control | Manual clone implementation needed |
Serializable | β
| β
| β | β | β | π« No control | Auto save/load over stream |
Externalizable | β | β
(manual) | β | β | β | β
Full control | Advanced I/O customization |
Comparable | β | β | β | β
| β | β
Full control | Natural sorting within class |
Comparator | β | β | β | β
| β | β
Full control | External or lambda-based sorting |