Programming foundations

Stage 4 · Lesson 5 of 10

Hash maps and sets

Data structures and Big-O

A stores key and value pairs. It runs each key through a , which turns the key into an array index. So lookup, insert and delete take O(1) on average.

Sometimes two keys land in the same slot, which is called a collision. The map keeps both, using or , and it grows its array as it fills so lookups stay fast. A set is the same idea with keys only.

This is the structure that most often turns a slow solution into a fast one. Checking "have I seen this?" in a list is O(n); in a set it is O(1). Keys must be (the language must be able to hash them), and should never change after they are inserted.

A question to check yourself, an AI practice session and 1 cheat sheet entry come when you add the roadmap to your account. Add it

Resources for this stage

Start with· follow one of these

Practise

Keep handy

  • Step through linked lists, hash tables and sorts with your own input

  • The Collections Framework section: ArrayList, LinkedList, HashMap, ArrayDeque

Also worth your time

Mark this stage as not done?