Map versus SortedMap
1 posts in topic
Flat View  Flat View

Posted By:   William_Morgan
Posted On:   Tuesday, June 25, 2002 09:24 AM

Will I get() an object from a TreeMap more quickly than from a HashMap. In other words, does TreeMap use the comparator function or the hashcode to locate objects.

Re: Map versus SortedMap

Posted By:   Paul_Gifford  
Posted On:   Tuesday, June 25, 2002 09:59 AM

Every Java developer should download the source code - it's very handy. From TreeMap.java:

private Entry getEntry(Object key) {
Entry p = root;
while (p != null) {
int cmp = compare(key,p.key);
if (cmp == 0)
return p;
else if (cmp < 0)
p = p.left;
p = p.right;
return null;

So TreeMap uses the comparator. Is it faster? That's a strange question, as HashMap and TreeMap are not used in the same way. TreeMap's entries are sorted; HashMap's are not. Looking at HashMap's get function, I would guess TreeMap is faster for retrieval, but certainly it's slower for insert.

About | Sitemap | Contact