To choose a Java collection, start from how you use the data: ArrayList for ordered data you read by index, HashMap and HashSet for fast lookup by key, ArrayDeque for queues and stacks, TreeMap and TreeSet when you need sorted order, and PriorityQueue when you always need the smallest or largest element next. Complexity tables back this up, but the access pattern decides first.
This guide helps you choose a java collection by comparing the common implementations on lookup, insertion, removal and ordering, and then walks through the decisions in practical order.
Before looking at Big O, answer these:
The answers narrow the field to one or two candidates.
| Need | Interface | First choice |
|---|---|---|
| Ordered, index access, duplicates | List | ArrayList |
| Unique items, fast membership test | Set | HashSet |
| Key to value lookup | Map | HashMap |
| First in, first out or stack | Deque / Queue | ArrayDeque |
| Sorted unique items | NavigableSet | TreeSet |
| Sorted keys | NavigableMap | TreeMap |
| Always take the smallest next | Queue | PriorityQueue |
The figures below are for the usual case on the standard library implementations.
| Collection | get / contains | add | remove | Notes |
|---|---|---|---|---|
ArrayList | O(1) by index, O(n) by value | O(1) amortised at the end, O(n) in the middle | O(n) | Contiguous array, cache friendly |
LinkedList | O(n) | O(1) at ends | O(1) at ends once positioned | Rarely the best choice |
ArrayDeque | O(n) by value | O(1) at both ends | O(1) at both ends | Best queue and stack |
HashSet / HashMap | O(1) average | O(1) average | O(1) average | No defined order |
LinkedHashSet / LinkedHashMap | O(1) average | O(1) average | O(1) average | Keeps insertion order |
TreeSet / TreeMap | O(log n) | O(log n) | O(log n) | Sorted, range queries |
PriorityQueue | O(n) by value, O(1) peek | O(log n) | O(log n) for head, O(n) arbitrary | Binary heap |
Hash based collections degrade only when many keys share a hash code. Since Java 8 a heavily colliding bucket turns into a tree, which keeps the worst case at O(log n) for keys that are Comparable.
Use ArrayList by default. It stores elements in an array, so index access is constant time and iteration is fast because memory is contiguous.
LinkedList looks attractive for insertion, but reaching the middle is O(n) and each node is a separate object, so in practice ArrayList is usually faster even for many inserts. If you need cheap work at both ends, use ArrayDeque instead.
Pass an expected size, new ArrayList<>(10_000), when you know it, to avoid repeated resizing. Use List.of for fixed contents and CopyOnWriteArrayList for rarely modified lists read by many threads.
HashMap and HashSet are the default for lookup. Pick a variant when ordering matters:
LinkedHashMap: iteration in insertion order; with accessOrder set to true it can back a simple LRU cache by overriding removeEldestEntry.TreeMap: keys sorted by natural order or a Comparator, with methods such as firstKey, floorKey and subMap.EnumMap and EnumSet: very fast and compact when keys are enum values.That range lookup is O(log n) and impossible with a plain HashMap. Remember that keys of hashed collections need correct and consistent equals and hashCode, and should be immutable while stored.
For a queue or stack, use ArrayDeque. It is faster than LinkedList and, unlike the legacy Stack class, is not synchronised on every call.
ArrayDeque does not allow null. For priority based processing use PriorityQueue with a comparator; note that iterating it does not return sorted order, only polling does.
The plain collections are not thread safe. For shared state choose:
ConcurrentHashMap for concurrent maps, not Hashtable or a fully synchronised wrapper;CopyOnWriteArrayList for read mostly lists;ConcurrentLinkedQueue, LinkedBlockingQueue or ArrayBlockingQueue for producer and consumer queues.Other traps to remember:
HashMap and HashSet iteration order is unspecified and may change between versions; never depend on it.ConcurrentModificationException; use Iterator.remove or removeIf.SequencedCollection, SequencedMap) so getFirst, getLast and reversed work on lists, deques, LinkedHashSet and LinkedHashMap.HashMap. Need sorted keys too? Use TreeMap. Need insertion order? Use LinkedHashMap.HashSet, LinkedHashSet or TreeSet by the same rule.ArrayList.ArrayDeque.PriorityQueue.List.of, Set.of or Map.of.Declare variables with the interface type, such as List<String> items = new ArrayList<>();, so you can switch the implementation later.
Rarely. Its constant time insert applies only when you already hold the position, such as through a ListIterator. For queues, ArrayDeque is almost always the better pick.
Lookup hashes the key to a bucket and compares entries there. If many keys collide in one bucket, the cost grows, though Java limits it to O(log n) with tree buckets for comparable keys.
Yes for hot paths. Big O ignores constants, memory layout and cache effects, so benchmark with a tool such as JMH before making a performance driven swap.
Use TreeSet. It keeps elements unique by comparison and sorted, with O(log n) operations.
Choose by access pattern first: ArrayList for sequences, HashMap and HashSet for lookup, LinkedHashMap for insertion order, TreeMap and TreeSet for sorted data, ArrayDeque for queues and stacks, and PriorityQueue for priorities. Use the concurrent classes for shared data, immutable factories for fixed data, and declare variables by interface so the choice stays easy to change.