TreeMap : Java Glossary
home T words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 2008-04-09 by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
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.

Learning More

Sun’s Javadoc on the TreeMap class : available:
Sun’s Javadoc on java.util.Map : available:

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.18] The information on this page is for non-military use only.
You are visitor number 17,236. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/treemap.html J:\mindprod\jgloss\treemap.html