Join waitlist
JavaCommunity question

HashMap bucket after Java 8 - linked list or tree?

Asked by Akash Pandey 1 month ago 4 answers 9 votes

I said linked list and it was wrong. what changed in Java 8

4 answers

Farhan Ali1 month agoMost voted8

It starts as a linked list, but when one bucket gets more than 8 entries it's converted to a red-black tree, so the worst case becomes O(log n). Only if the table capacity is at least 64 though, below that it resizes instead.

Farhan Ali1 month ago3

@Akash with a small table collisions are more likely just because the table is small. resizing spreads them out and trees use more memory per node

Gayathri Menon1 month ago2

and know the equals/hashCode contract, that's always the next question

Akash Pandey1 month ago

why resize first?

Related questions

Go deeper

Want the full explanation, not just an answer?

Structured lessons on DSA, core CS, Java and interview skills, each ending in a quiz so you know it stuck.