Collections

Pick the right box, and know what's inside it. HashMap internals, ArrayList vs LinkedList, sets, ConcurrentHashMap, fail-fast iterators and Comparable vs Comparator.

Topics in this section

The collections family

Three kinds of container. A List is the queue at a ticket counter: order matters, duplicates are fine. A Set is a guest list: nobody gets in twice. A Map is a coat check: hand in a ticket, get your coat back.

How HashMap works inside

A HashMap is a row of lockers. The key's hash picks the locker number. If two keys get the same locker they share it in a little chain. When the lockers are 75% full, the building doubles in size and everyone moves.

ArrayList vs LinkedList

ArrayList is a row of numbered seats: jump straight to seat 500, but squeezing someone in at the front makes everyone shuffle along. LinkedList is a conga line: easy to join in the middle once you're there, but finding person 500 means counting from the start.

HashSet, LinkedHashSet, TreeSet

All three are guest lists with no duplicates. HashSet is a bag: fast, no order. LinkedHashSet remembers who arrived first. TreeSet keeps everyone sorted.

Comparable vs Comparator

Comparable is an object's own natural way to line up, like sorting people by the date of birth on their ID. A Comparator is an outside judge you bring in for a different contest: by height today, by name tomorrow.

Fail-fast vs fail-safe iterators

A fail-fast iterator is a strict librarian: rearrange the shelf while they're counting and they stop and shout. A fail-safe iterator counts from a photo of the shelf, so your changes don't disturb them — and don't show up either.

Each topic pairs a plain-language explanation with code, interview questions, and related practice where available.