Hash, Sets, and Maps

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/55

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

56 Terms

1
New cards

What is Hashing?

It’s the process of converting a key into another value (a hash value) using a hash function to allow fast data lookup.

2
New cards

Hash Function

A mathematical function that takes a key and produces a fixed-size hash value (an integer).

3
New cards

Hash Table / Hash Map

A data structure that stores key-value pairs and uses a hash function to map each key to an index for quick access.

4
New cards

What does a Hash Table do?

It maps keys to values using a hash function, allowing fast insertion, search, and deletion.

5
New cards

Why use Hash Tables over Arrays or Linked Lists?

Because hash tables allow direct access to data using a key, making operations faster than searching through arrays or lists.

6
New cards

Real-life analogy for Hashing

Like a magical library system where a “machine” (hash function) tells you exactly which shelf (index) a book (value) is on.

7
New cards

Key-Value Pair

Each item in a hash table consists of a key and a value (e.g., key: “name”, value: “Kimmy”).

8
New cards

Formula to get the index

index = hash(key) % array_size or h(k) = k mod m.

9
New cards

Bucket

A storage location (array slot) in the hash table that holds key-value pairs.

10
New cards

What happens during Retrieval?

The hash function is used again on the key to locate its bucket and return the stored value.

11
New cards

What Java class represents a hash table?

HashMap — a built-in Java class implementing the Map interface.

12
New cards

Insertion operation

put(key, value) — adds a new key-value pair.

13
New cards

Retrieval operation

get(key) — returns the value of a given key.

14
New cards

Checking for a key

containsKey(key) — checks if the key exists.

15
New cards

Deletion operation

remove(key) — removes a key-value pair.

16
New cards

What is Table Design?

The way the hash table is structured, sized, and organized to minimize collisions and improve efficiency.

17
New cards

Why is proper table design important?

To ensure even key distribution, minimal collisions, and efficient memory use.

18
New cards

If the table size (m) is too small…

Too many collisions occur, slowing performance.

19
New cards

If the table size (m) is too large…

Wastes memory.

20
New cards

Why use a prime number for table size?

Prime numbers reduce clustering when using h(k) = k mod m.

21
New cards

What is resizing (rehashing)?

Expanding the table when it becomes too full and recomputing all hash values.

22
New cards

What is a load factor?

The ratio of stored entries to the table size (e.g., 0.75). It helps decide when to resize.

23
New cards

What makes a good hash function?

It must be deterministic, distribute keys evenly, and compute quickly.

24
New cards

What happens if the hash function clusters keys?

Collisions increase, causing slower lookups.

25
New cards

Bucket

Each position in the hash table used to store key-value pairs.

26
New cards

Two main methods of storage

Chaining and Open Addressing.

27
New cards

Chaining definition

Each bucket contains a list (or tree) of all key-value pairs that share the same hash index.

28
New cards

Advantages of Chaining

Simple to implement and handles many collisions well.

29
New cards

What data structures can chaining use?

Linked lists or balanced trees (like in Java HashMap).

30
New cards

Open Addressing definition

Stores all elements directly in the table; if a collision happens, it searches for the next empty slot.

31
New cards

Why is Open Addressing used?

When there are space restrictions (no extra linked lists).

32
New cards

Linear Probing

Checks the next slot sequentially until an empty one is found.

33
New cards

Quadratic Probing

Moves through slots in increasing quadratic steps to avoid clustering.

34
New cards

Double Hashing

Uses a second hash function to determine the step size for searching.

35
New cards

Rehashing process

Creates a new, larger table, recomputes hashes, and reinserts all keys.

36
New cards

What does load factor measure?

How full a hash table is (stored elements / number of buckets).

37
New cards

If load factor is high…

The table is crowded; collisions increase → need rehashing.

38
New cards

If load factor is low…

The table is underused but performs efficiently.

39
New cards

What is a Collision?

When two different keys map to the same bucket index.

40
New cards

How are collisions handled?

Using Chaining or Open Addressing.

41
New cards

Why are collisions unavoidable?

Because multiple keys can generate the same hash value.

42
New cards

Set (Data Structure)

Stores a collection of unique elements; no duplicates allowed.

43
New cards

Unordered Set

Implemented with a hash table; elements have no order and operations are fast (constant time on average).

44
New cards

Ordered Set

Implemented with a balanced binary search tree (BST); keeps elements sorted.

45
New cards

What happens when you add a duplicate to a Set?

It’s ignored — only unique elements are kept.

46
New cards

HashSet

Uses hashing; elements are stored based on hash codes, not in order.

47
New cards

TreeSet

Uses a binary search tree; keeps elements sorted (ascending order).

48
New cards

When to use Sets

When you need to store unique elements efficiently, like in algorithms or data analysis.

49
New cards

Map (Data Structure)

Stores key–value pairs, where each key is unique and maps to exactly one value.

50
New cards

Why use a Map instead of an array or list?

You can retrieve data directly using a key instead of searching by index.

51
New cards

Key Features of Maps

  • Unique keys - Fast access - Dynamic size - Key-value pairing
52
New cards

Common Map Operations

Insert (put), Retrieve (get), Check (containsKey), Delete (remove).

53
New cards

HashMap

Uses a hash table; stores key-value pairs with no guaranteed order.

54
New cards

TreeMap

Uses a binary search tree; stores keys in sorted order.

55
New cards

LinkedHashMap

Uses hashing + linked list; preserves insertion order.

56
New cards

What does re-inserting an existing key do in a Map?

Updates the value of that key.