TreeMap

A TreeMap uses the Map interface, which means it stores items as key-value pairs.

They are like the HashMap. However, a HashMap, the TreeMap stores its items sorted by the natural ordering of its keys. This is due to the differing means of storage each type of collection uses internally. The HashMap stores its keys as hashes for lookup, whereas the TreeMap stores its keys in a binary tree structure for lookup. The tradeoff for this natural ordering of the TreeMap is that its operations are slower than a HashMap.

Syntax

import java.util.TreeMap
TreeMap<KeyDatatype, ValueDatatype> myTreeMap = new TreeMap<KeyDatatype, ValueDatatype>();

The TreeMap class comes from the java.util package. Therefore, it must be imported in order to be used. The TreeMap is initialized with two generic types inside angle brackets < ... >. The generic data types for KeyDatatype and ValueDatatype can either be different or the same.

Accessing Items

Keys are used for uniquely identifying a value in a TreeMap. This allows for efficient data storage and easy access. In the example below, the course names are the keys and the teachers assigned are the values that can be accessed by passing the corresponding key into the .get() method.

import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<String, String> courseTeacher = new TreeMap<String, String>();
courseTeacher.put("History", "Ben");
courseTeacher.put("Mathematics", "Jeanette");
courseTeacher.put("Physics", "Lily");
System.out.println(courseTeacher.get("Physics"));
System.out.println(courseTeacher.get("History"));
}
}

The following will be printed in the output below:

Lily
Ben

Adding Items

Items can be added to a TreeMap using the .put() method. It accepts two parameters, a key and a value, and stores them as a pair ({ key=value }).

import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<String, String> courseTeacher = new TreeMap<String, String>();
// Add keys and values (CourseNames, Teacher)
courseTeacher.put("History", "Ben");
courseTeacher.put("Mathematics", "Jeanette");
courseTeacher.put("Physics", "Lily");
System.out.println(courseTeacher);
}
}

This will output the following “course=teacher” assignments:

{History=Ben, Mathematics=Jeanette, Physics=Lily}

Removing Items

Items can be removed from a TreeMap using the .remove() method. It accepts one parameter, the key, and removes the corresponding key-value pair from the HashMap.

import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<String, String> courseTeacher = new TreeMap<String, String>();
courseTeacher.put("History", "Ben");
courseTeacher.put("Mathematics", "Jeanette");
courseTeacher.put("Physics", "Lily");
courseTeacher.remove("Physics");
System.out.println(courseTeacher);
}
}

This will output the following:

{History=Ben, Mathematics=Jeanette}

Removing All Items

The .clear() method can be used to remove all the items from the TreeMap.

import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<String, String> courseTeacher = new TreeMap<String, String>();
courseTeacher.put("History", "Ben");
courseTeacher.put("Mathematics", "Jeanette");
courseTeacher.put("Physics", "Lily");
courseTeacher.clear();
System.out.println(courseTeacher);
}
}

The emptied TreeMap will be displayed in the output below:

{}

Traversing a TreeMap

A TreeMap can be traversed with the for-each loop. The .keySet() method can be used to obtain only the keys while the .values() method can be used to obtain only values.

import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<String, String> courseTeacher = new TreeMap<String, String>();
courseTeacher.put("History", "Ben");
courseTeacher.put("Mathematics", "Jeanette");
courseTeacher.put("Physics", "Lily");
System.out.println("Courses offered at our Institute:");
for (String i : courseTeacher.keySet()) {
System.out.println(i);
}
System.out.println("\nTeachers teaching at our Institute:");
for (String i : courseTeacher.values()) {
System.out.println(i);
}
}
}

The following output will look like this:

Courses offered at our Institute:
Mathematics
History
Physics
Teachers teaching at our Institute:
Jeanette
Ben
Lily

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Java on Codecademy