TreeMap : Java Glossary

TreeMap
One of the Collection classes. It lets you access the elements in your collection by key, or sequentially by key. It has considerably more overhead than ArrayList or HashMap. Use HashMap when you don’t need sequential access, just lookup by key. Use an ArrayList and use Arrays. sort if you just want the elements in order. TreeMap keeps the elements in order at all times. With ArrayList you just sort when you need to.

With TreeSets the key must be embedded in the object you store in the collection. Often you might have TreeSet of Strings. All you can do then is tell if a given String is in the Set. It won’t find you an associated object he way a Treemap will. With a TreeMap the keys and the objects they are associated with are separate.

TreeMap and its brother TreeSet oddly have nothing to do with representing trees. Internally they use a tree organisation to give you an alphabetically sorted Map/Set, but you have no control over links between parents and children.

Internally TreeMap uses red-black trees. There is no need to presort the data to get a well-balanced tree. On the other hand, if the data are sorted (ascending or descending), it won’t hurt as it does with some other types of tree.

The TreeMap will be sorted by the natural order of the key, i.e. by its implemented Comparable interface. If you want to keep it sorted in some other order, implement a Comparator< TypeOfKey> and pass that to the TreeMap constructor.

If you don’t supply a Comparator to define the ordering you want, TreeMap requires a Comparable implementation on the item class to define the natural order.

When you don’t know the precise key, you can use lowerKey, ceilingKey and higherKey to find it. You can also logically extract subsets of the map on a range of keys with subMap.

Learning More

Oracle’s Javadoc on TreeMap class : available:
Oracle’s Javadoc on java.util.Map class : available:
Oracle’s Javadoc on Treemap.lowerKey : available:
Oracle’s Javadoc on Treemap.ceilingKey : available:
Oracle’s Javadoc on Treemap.higherKey : available:
Oracle’s Javadoc on Treemap.subMap : available:

CMP homejump to top

available on the web at:

http://mindprod.com/jgloss/treemap.html
  

optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\treemap.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Blog
IP:[65.110.21.43]
Your face IP:[107.21.156.140]
You are visitor number 37,538.