Hash Map Flashcards

1
Q

Show me how to create a hash map

A

Map<Integer, Integer> map = new HashMap<>();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Show me what import you need for a hash map

A

java.util.*

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Show me how to store a key value pair?

A

map.put(key, value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Show me how to get the value of a key?

A

map.get(key)

Will get the associated value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a hashmap?

A

A HashMap is a data structure that stores key-value pairs and allows for fast lookups, insertions, and deletions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Best data structure?

A

Arrays or array lists

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why can the big O complexity sometimes be O(n)? And is that the worst case of a hashmap

A

Yes, big O worst case of hashmap is O(n)

It can get like that if too many elements hash to the same index, the hashmap degenerates into a linked list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does put method do in a Hashmap?

A

put(key, value)

Updates the value if the key exists

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does get method do in a Hashmap?

A

get(key)

Gets the value of the key

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does entrySet do in a Hashmap?

A

Returns a set of all key-value pairs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does values() do in a hashmap?

A

Return a collectio nof all values in the hash map

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does keySet() do in a hashmap?

A

Returns a set of all keys in the hashmap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does a hash map work internally?

A
  • Computes a hash using hashCode() from the key
  • maps the hash to an index in an array (buckets)
  • If collision occurs, uses a linked lists or uses a balanced tree to fix this
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How is a hashmap different from a hashset?

A

Hashmaps store key value paris while hashsets store unique elements
- Keys are unique but values can repeat in a Hashmap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly