Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Perfomance question on collection

822570Dec 9 2010 — edited Dec 25 2010
Hello, all.
There is a question: if we have to get some element of collection by its value, what is faster: TreeSet or HashSet?
We are talking only on speed while getting element, so we consider that perfomance leaks(placing and reording) on adding element in
TreeSet is not actual in this topic.

As I understand treeset will just compare value of searching item with every element in path throught tree using binary search and HashSet will just compute hash code of element value and findout its index. I have an opinion, that TreeSet will faster only under some size of collection, but then due to huge tree path counting hash code will be faster.

I just write a simple test for this question and it show that Tree Set it faster, but I used only 100 000 elements in collection. But in theory time for search in tree set is proportial to size of collection (but I don`t know in what function may be O(ln n)?), but HashSet need just time to calculate hash code and index, and I suppose it cann`t be so depending on size of collection(so funtion should be just like a constant). What can you say on this question in thery and in practice?

P.S. small offtop: what`s about old sun java forum? As far as I understand this is new place for it? Can old profiles from sun forum restore here?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 22 2011
Added on Dec 9 2010
37 comments
533 views