31

Could someone tell me when and why to use TREEMAP.I went through This link but didn't find my answer.

As Per my thinking we use treemap to get the data sort according to your key and the same we can achieve by other ways also.

Java_Alert
  • 1,159
  • 6
  • 24
  • 50
  • 1
    There are other ways, but this is the easiest. What other way do you propose having key value pairs sorted? – jlordo Dec 07 '12 at 11:09
  • "As Per my thinking we use treemap to get the data sort according to your key and the same we can achieve by other ways also." - EXACTLY! – xagyg Dec 07 '12 at 11:20
  • @jlordo comparable and comparator interfaces. – Java_Alert Dec 10 '12 at 05:45
  • 1
    @Sunil to have the key values pairs sorted in the TreeMap the Keys must be `Comparable`, and you still don't show me anything easier than a TreeMap to have sorted (by key) key value pairs. – jlordo Dec 10 '12 at 05:56
  • @jlordo that's gud but suppose i want to sort my data on the basis of values instead of keys than as per my thinking TreeMap will not worth to use.Please correct me if i am wrong.Can you pls tell me in that case how we can use TREEMAP. – Java_Alert Dec 10 '12 at 06:05
  • 1
    if you have a 1:1 relation between your keys and values, you can use the values as keys and the keys as values, if not, I don't think there's a good solution for you, not even in Guava, which may be a reason for you to overthink your data design decisions. Why/for what would you need key value pairs, sorted by values? (You could implement a class like `MySortedSet extends HashSet>` and provide a Comparator fitting your needs. – jlordo Dec 10 '12 at 06:26

4 Answers4

35

Let's say you want to implement a dictionary and print it in alphabetical order, you can use a combination of a TreeMap and a TreeSet:

public static void main(String args[]) {
    Map<String, Set<String>> dictionary = new TreeMap<>();
    Set<String> a = new TreeSet<>(Arrays.asList("Actual", "Arrival", "Actuary"));
    Set<String> b = new TreeSet<>(Arrays.asList("Bump", "Bravo", "Basic"));

    dictionary.put("B", b);
    dictionary.put("A", a);

    System.out.println(dictionary);
}

All the sorting is done automatically and it prints:

{A=[Actual, Actuary, Arrival], B=[Basic, Bravo, Bump]}

You could have sorted the structures manually too of course but using TreeMap/Set can be more efficient, reduces the number of lines of code (= the number of bugs) and is more readable.

assylias
  • 321,522
  • 82
  • 660
  • 783
9

It is efficient way of having objects sorted by some key. If also random access is important for you then TreeMap is the answer. With this data structure you can iterate in order.

If random access is not needed then rather use sorted set/bag or list.

Why is there no SortedList in Java?

Aleksander Gralak
  • 1,509
  • 10
  • 26
5

The javadoc you link to, clearly states that it is an implementation of navigable and sorted map interfaces. You would use it when you need this functionality.

Community
  • 1
  • 1
David Soroko
  • 8,521
  • 2
  • 39
  • 51
4

TreeMap

A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to Algorithms.

Use this data-structure when you need ordered key not only ascending you can pass comparator to constructor TreeMap(Comparator<? super K> comparator) to write your own sorting logic. As well it is a type of self-balancing binary search tree.

Community
  • 1
  • 1
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103