Java Data StructuresLesson 71Free preview
Java TreeMap
This lesson is part of Java · All levels
Summary
TreeMap stores keys and values in a balanced binary search tree so that keys are always in sequence. Its navigation features include firstKey(), lastKey(), and subMap makes it suitable for banded or range-based data. Just like TreeSet, the key to TreeMap must be Comparable or given a Comparator.
Key points
- The keys are always sorted according to natural ordering or a Comparator
- The main operation get, put, and remove is running
- Provide firstKey(), lastKey(), higherKey(), lowerKey(), and subMap()
- Do not allow the key to be used normally
- The entrySet() iteration results in a key in ascending order
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.