Reputation: 1009
I want to search efficiently for an object in a HashSet that I have.
I would like to know if the contains() method which is defined on java collections uses binary search or not? Or should I write my own binary search algorithm?
Upvotes: 3
Views: 1494
Reputation: 120968
The general search complexity in a HashSet
is O(1)
- meaning it is constant. Writing your own? Better then this?
You absolutely can look at the sources, understand that a HashSet
is actually a HashMap
internally; that it uses buckets and LinkedNodes and TreeNodes; understand how these work, etc etc. Or trust the implementation that is good and focus on other stuff; unless you honestly have a requirement for something faster.
Upvotes: 4