SwadhyAI Notes

All Java notes

Java

Java collections

›Short versionOpen if you only have a minute.
  • The framework splits a contract (List, Set, Queue, Map) from the class that implements it.
  • Declare the variable as the interface. Construct ArrayList, HashSet, or HashMap unless you need a stronger promise.
  • Map stores pairs and stands beside Collection. Walk it through keySet, values, or entrySet.
  • Cost lives on the class. get on ArrayList is constant time. get on LinkedList is linear.
  • Standard iterators are fail-fast: a structural change outside the iterator throws ConcurrentModificationException.
  • Equality for hash collections is equals plus hashCode. Equality for tree collections is comparison.

A method that accepts “some group of strings” should say which questions it will ask. The gives those questions names: , , queue, and .

Each name is an . The class you construct — ArrayList, HashSet, TreeMap — is one way to keep the promise, with a particular cost. Callers that only need the promise should see the interface.

On this page

1

Contract vs class

List, Set, Map name what you may ask.

2

Map beside Collection

Pairs and the three live views.

3

What you construct

Hash, tree, and linked hash trade-offs.

What the interface promises

  • List: order, duplicates, get by index.
  • Set: no second equal element; no index.
  • Map: unique keys to values; put replaces on equal key.

What the class decides

  • ArrayList vs LinkedList: cost of get inside a loop.
  • Hash vs tree: constant average vs logarithmic; equals/hashCode vs comparator.
  • Fail-fast iterators: do not structurally change the backing collection while iterating.

Pick the contract first

NeedDeclare asUsual construction
Ordered elements, duplicates OKListArrayList (LinkedList if deque-heavy)
Unique elements, no order requirementSetHashSet (LinkedHashSet for insertion order)
Key to value lookupMapHashMap (TreeMap for sorted keys)
Caller sees the interface; you choose the implementation
Diagram text: the exact Mermaid definition below preserves the nodes, labels, and connections. Enable JavaScript to view the rendered diagram.
Read the diagram definition
sequenceDiagram
  participant Caller
  participant List as List interface
  participant Impl as ArrayList
  Caller->>List: add, get, iterate
  List->>Impl: delegate
  Impl-->>List: behavior + cost
  List-->>Caller: contract only

Where Map sits

is the root for groups of single elements. List, Set, and Queue extend it. Map does not. A map is a group of pairs, and the framework keeps that distinction in the type hierarchy so you cannot pass a map where a collection of elements is required.

The three views are the bridge. keySet() is a set of keys. values() is a collection of values and may contain duplicates. entrySet() is a set of key-value entries. They are live: removing from the view removes from the map. Structural changes through one of them are still visible to a fail-fast iterator on another.

What you construct

Hash-based classes answer contains and get in average constant time and rely on a stable equals and hashCode. Tree classes answer in logarithmic time and rely on a comparator that treats comparison-equal keys as the same key. Linked hash classes add a chain so iteration follows insertion, and LinkedHashMap can retie that chain on access.

Queue and Deque sit on this same framework. For a plain double-ended queue, ArrayDeque is the usual class. LinkedList implements Deque too, and it is the heavier way to get the same end operations.

Framework.java
Collection<String> bag = new ArrayList<>();
bag.add("ada");

Map<String, Integer> scores = new HashMap<>();
scores.put("ada", 10);
Set<String> keys = scores.keySet();
keys.remove("ada"); // the map loses that entry too

A method is declared to take a Collection. Can the caller pass a HashMap? What can they pass instead?

A HashMap is a Map, so it is not a Collection and will not compile as that argument. Pass scores.keySet(), scores.values(), or scores.entrySet(), choosing the view that holds the elements the method should see.

Principal-engineer rule

Name the variable with the smallest interface that matches the questions your method asks. Pick the implementation for cost and ordering, and never pass a Map where a Collection of elements is required — use a view instead.

Good default: List with ArrayList, Set with HashSet, Map with HashMap; switch to linked-hash or tree variants only when iteration order or sorted keys are part of the contract.