[jdom-interest] Attributes as HashMap?

Alex Chaffee guru at edamame.stinky.com
Wed Oct 4 12:22:23 PDT 2000

>   I have a general question:
>   Is there particular reason why choose a LinkedList as attributes in Element, not use a HashMap directly, even an attribute is a name-value pair (as in the java.util.Map)?

Speculating on performance is dangerous, but...

Element needs to store its Attributes in a List, since List is
ordered, and Map is not.

That means we'd need to add an extra Map object for indexing.  This
has performance implications on memory and GC (GC takes time, and will
eventually happen in a server; with an internal Map there'd be more
garbage to collect).

Furthermore, you have to consider the time it takes to add the entry,
and to search for it.  Calling hashCode() takes time, and in a small
list (say, <10 items), it can be faster to iterate and compare N times
than to hash and compare once.  Since most elements have a very small
number of attributes, the extra expense of an index hashtable is not
worth it.

My gut feeling.  Feel free to prove me wrong empirically :-)

 - Alex

P.S. It might be nice to have Element.getAttributeMap() that builds a
Map, for API convenience.

Alex Chaffee                       mailto:alex at jguru.com
jGuru - Java News and FAQs         http://www.jguru.com/alex/
Creator of Gamelan                 http://www.gamelan.com/
Founder of Purple Technology       http://www.purpletech.com/
Curator of Stinky Art Collective   http://www.stinky.com/

More information about the jdom-interest mailing list