TreeMap in Java: Use & Example

Instructor: David Gloag
In this lesson, we'll take a look at a TreeMap in Java and see what it's used for and go through an example. At the end, you should have a good understanding of this important element in Java.

The Space/Speed Tradeoff

Computer programmers are always looking for ways to optimize their code. The reasons for this vary. Sometimes it's about space, as the environment is resource restricted such as in mobile devices. In others, it's about speed as there is simply a lot of data that needs to be processed. For the rest, it's a combination of the two. Programming in the Java language is no different. Fortunately, there are ways in this language to address these types of situations. These allow the programmer to optimize their programs for the job at hand. One of these ways is to use a TreeMap.

What is a Java TreeMap?

A Java TreeMap is a class that is available for use in the Java computer programming language. A class is a collection of information and the operations that can be used to manipulate that information. The TreeMap class has a number of operations (20+) associated with it. Some of the notable ones include:

  • TreeMap() - This is one of the four constructors. This operation creates a new TreeMap that sorts values in the default order.
  • TreeMap(Comparator comp) - This is another of the four constructors. This operation creates a new TreeMap that sorts values according to the comparator.
  • clear() - This operation removes all entries from a TreeMap and resets the TreeMap to empty.
  • put() - This operation associates a value with a specific key in the TreeMap.
  • remove() - This operation removes a specific entry from the TreeMap.

What is a Java TreeMap Used For?

Java TreeMaps are used to hold a sequence of map entries for use within a computer program. These map entries consist of key/value pair and are sorted in an ascending fashion according to the relative relationships of the keys. The sort order can be left as the default, or specified through one of the constructors. Either way, the TreeMap provides an efficient method to store and retrieve the information contained within. If you think about it, TreeMaps have a number of uses in everyday life. They may be used to store employee records or perhaps the contact list on your cell phone. In fact, they are useful any place where the information needs to be sorted and where quick random access is necessary.

TreeMap Example

Okay, now that we've talked about some of the details, let's take a look at a specific example. Let's say we have key/value pair that consists of an account number and a name. Further, let's say it contains the following: (3, Jennifer), (4, Mark), (1, Toby), (6, Gail), (2, Ruth), (5, Dan). A TreeMap of this information might look like:

Java TreeMap Example

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account