Collections Framework 2

Lesson 17 of 20

Keys, buckets and no duplicates — put and get your way through HashMap and HashSet in one interactive tour.

The big picture

Look things up by key

A HashMap maps each key to exactly one value; a HashSet is the same machinery holding keys only — and never a duplicate.

HashMap = dictionary

put(key, value) stores, get(key) looks up — no scanning, the key decides where to look.

Keys are unique

put with an existing key REPLACES its value. One key, one entry, always.

hashCode finds buckets

The key is hashed into a bucket index; equals() settles ties inside the bucket.

HashSet = keys only

add(x) quietly does nothing if x is already present — duplicates simply cannot exist.