Sobes.tech
Junior — Middle

Have you used the TreeMap data structure in your projects?

sobes.tech AI

Answer from AI

Yes, I have used the TreeMap data structure in Java projects. TreeMap is an implementation of the SortedMap interface that stores key-value pairs in sorted order by key. It is useful when you need to maintain ordered data and perform search, insert, and delete operations quickly.

For example, TreeMap is useful for storing settings or cache where the order of keys is important:

import java.util.TreeMap;

TreeMap<String, Integer> map = new TreeMap<>();
map.put("apple", 3);
map.put("banana", 2);
map.put("orange", 5);

// Keys will be sorted alphabetically
for (String key : map.keySet()) {
    System.out.println(key + ": " + map.get(key));
}

TreeMap provides logarithmic time access and supports methods for obtaining sub-maps within a range of keys.