JavaTreeMap
Java TreeMap
ATreeMap is a collection that stores key/value pairsin sorted order by key.
It is part of thejava.util package and implements theMap interface.
Tip: UnlikeHashMap, which does not maintain order,TreeMap keeps its keys sorted.
Create a TreeMap
Create aTreeMap that storesString keys andString values:
Example
import java.util.TreeMap; // Import the TreeMap classTreeMap<String, String> capitalCities = new TreeMap<>(); Now you can use methods likeput(),get(), andremove() to manage sorted key/value pairs.
Add Items
Use theput() method to add key/value pairs:
Example
import java.util.TreeMap;public class Main { public static void main(String[] args) { TreeMap<String, String> capitalCities = new TreeMap<>(); capitalCities.put("England", "London"); capitalCities.put("India", "New Dehli"); capitalCities.put("Austria", "Wien"); capitalCities.put("Norway", "Oslo"); capitalCities.put("Norway", "Oslo"); // Duplicate capitalCities.put("USA", "Washington DC"); System.out.println(capitalCities); }}Output: The keys are sorted alphabetically (e.g., {Austria=Wien, England=London, India=New Dehli, Norway=Oslo, USA=Washington DC}).
Note: Duplicates like "Norway" will only appear once.
Access an Item
Useget() with the key to access its value:
Remove Items
Useremove() to delete a key/value pair by key:
Useclear() to remove all items:
TreeMap Size
Usesize() to count the number of key/value pairs:
Note: The size only counts unique keys. If a key is added more than once, only the latest value is kept.
Loop Through a TreeMap
Loop through the items of aTreeMap with a for-each loop.
Note: Use thekeySet() method if you only want the keys, and use thevalues() method if you only want the values:
Example
// Print keysfor (String i : capitalCities.keySet()) { System.out.println(i);}Example
// Print valuesfor (String i : capitalCities.values()) { System.out.println(i);}Example
// Print keys and valuesfor (String i : capitalCities.keySet()) { System.out.println("key: " + i + " value: " + capitalCities.get(i));}TreeMap vs HashMap
| Feature | HashMap | TreeMap |
|---|---|---|
| Order | No guaranteed order | Sorted by keys |
| Null Keys | Allows one null key | Does not allow null keys |
| Performance | Faster (no sorting) | Slower (maintains sorted order) |
Tip: UseHashMap for performance, andTreeMap when you need sorted keys.
The var Keyword
From Java 10, you can use thevar keyword to declare aTreeMap variable without writing the type twice. The compiler figures out the type from the value you assign.
This makes code shorter,but many developers still use the full type for clarity. Sincevar is valid Java, you may see it in other code, so it's good to know that it exists:
Example
// Without varTreeMap<String, String> capitalCities = new TreeMap<String, String>();// With varvar capitalCities = new TreeMap<String, String>();The Map Interface
Note: Sometimes you will see bothMap andTreeMap in Java code, like this:
import java.util.Map;import java.util.TreeMap;Map<String, String> capitalCities = new TreeMap<>();This means the variable (capitalCities) is declared as aMap (the interface), but it stores aTreeMap object (the actual map). SinceTreeMap implements theMap interface, this is possible.
It works the same way, but some developers prefer this style because it gives them more flexibility to change the type later.

