Hashmap Flashcards

1
Q

How does hashmap work

A

It stores key value pair and works based on the principle of hashing

Use hashing algorithm to generate hash code and compare with the hashcode() and equals() method

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

What does hashmap store in the bucket (value)

A

It store both key and value

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

What happens if two different. Hashmap key objects have same hashcode

A

Stored on same bucket in a linked list and the key equals method will help identify which entry in the list

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

Hashmap, treemap and linkedhashmap

A

Hashmap - O (1) insertion and lookup. No order for keys
Treemap - O(log n) insertion and lookup . Keys are ordered in sorted manner.
Linkedhashmap - O(1) insertion and lookup. Keys are ordered by insertion order

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

Iterate through a map

A

for (Map.Entry entry : map.entrySet())
System.out.println(“Key = “ + entry.getKey() +
“, Value = “ + entry.getValue());

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

Sort by descending order

A

Collections.sort(result, (r1,r2) -> map.get(r1).equals(map.get(r2)) ?
r1.compareTo(r2) : map.get(r2).compareTo(map.get(r1)));

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