Yes, custom objects can be used as keys in a TreeMap. However, unlike HashMap, TreeMap must be able to compare and sort its keys. Therefore, the key class must either implement the Comparable interface or a Comparator must be provided while creating the TreeMap. Otherwise, a ClassCastException will occur at runtime.
Key Points: • TreeMap stores keys in sorted order. • Custom key objects must be comparable. • Sorting can be achieved using Comparable or Comparator. • TreeMap uses comparison logic instead of hashCode() and equals() for ordering. • If no comparison mechanism is available, TreeMap cannot determine the order of keys.
Why Does TreeMap Need Comparable or Comparator?
TreeMap is internally implemented using a Red-Black Tree.
To insert a new key, TreeMap must determine:
• Whether the key is smaller or larger than existing keys. • Where the key should be placed in the tree.
Without comparison logic, TreeMap cannot maintain sorted order.
Using Comparable
The key class can implement Comparable.
Example:
class Employee implements Comparable<Employee> {
private int id;
public Employee(int id) {
this.id = id;
}
@Override
public int compareTo(Employee other) {return Integer.compare(
this.id,
other.id);
}
}Now Employee objects can be used directly as TreeMap keys.
Using Comparator
If modifying the class is not possible, a Comparator can be supplied.
Example:
TreeMap<Employee, String> map = new TreeMap<>(
Comparator.comparing(
Employee::getId));This allows custom sorting logic without changing the class.
Example: Suppose we want employee records sorted by employee ID.
Employee:
103 → Mike
101 → John
102 → David
TreeMap Output:
101 → John
102 → David
103 → Mike
Code Example:
import java.util.TreeMap;
class Employee
implements Comparable<Employee> {
private int id;
Employee(int id) {
this.id = id;
}
@Override
public int compareTo(Employee other) {return Integer.compare(
this.id,
other.id);
}
@Override
public String toString() {
return String.valueOf(id);
}
}
public class Demo {
public static void main(String[] args) {
TreeMap<Employee, String> map =
new TreeMap<>();
map.put(new Employee(103), "Mike");
map.put(new Employee(101), "John");
map.put(new Employee(102), "David");
System.out.println(map);
}
}Output:
{101=John, 102=David, 103=Mike}
What Happens If Comparable or Comparator Is Missing?
Example:
class Employee {
private int id;
Employee(int id) {
this.id = id;
}
}
TreeMap<Employee, String> map =
new TreeMap<>();
map.put(new Employee(101), "John");Runtime Result:
ClassCastException
Reason:
TreeMap does not know how to compare Employee objects.
TreeMap vs HashMap for Custom Objects
HashMap:
• Uses hashCode() and equals() • No sorting required • Custom objects can be keys if hashCode() and equals() are implemented properly
TreeMap:
• Uses compareTo() or Comparator • Maintains sorted order • Requires comparison logic for custom objects
Real-World Use Cases
Using custom objects as TreeMap keys is useful when:
• Employee records must be sorted by ID • Products must be sorted by price • Students must be sorted by rank • Transactions must be sorted by date
Interview Tip: A concise interview answer is:
"Yes, custom objects can be used as keys in a TreeMap, provided the objects can be compared. This is typically achieved by implementing the Comparable interface or supplying a Comparator. Since TreeMap maintains keys in sorted order using a Red-Black Tree, it must be able to compare keys to determine their position in the tree."