[jdom-interest] AttributeList performance

Victor Toni victor.toni at ebuconnect.de
Thu Apr 28 08:15:27 PDT 2005

Elliotte Harold wrote:

> Victor Toni wrote:
>> Maybe something like this (which could be still optimized if called 
>> often):
>>     public final int hashCode() {
>>         return name.hashCode() * 29 + namespace.hashCode();
>>     }
> This would be inappropriate for a general purpose hashCode method 
> because it would lead to numerous hash collisions. 

Correct. It would be appropriate for qualified names. For an Attribute 
it should include the value.

> For instance, all the id attributes in every document would have the 
> same hash, regardless of their distinct values.

Right, but usually this is only important when collecting all attributes 
in a document(XPath, validation, etc.). This should be less regular then 
getting certain attributes from an element.

> A better solution would be to use the attributes' qualified names 
> (unique within any one attribute) as the key into the hash table 
> rather than the Attribute objects themselves.

> Again, however, I doubt the overhead justifies the "optimization". 
> Barring hard evidence to the contrary, I think the the current 
> approach is faster and should be preferred.
Therefore I suggested an additional class for qualified names. It would 
help creating other access mechanisms. The current 
Element.getAttribute(name) is O(N) but could be O(log(n)). Surely this 
change would be mainly for cases where there are a lot of atrributes but 
it would hurt simple documents?

More information about the jdom-interest mailing list