Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Print treemap in reverse order

Tags:

In my assignment we are read from a file the text:

To be, or not to be: that is the question:
Whether 'tis nobler in the mind to suffer

then count the times each has occured. I've been able to print this map unsorted, then I was able to make a TreeMap and print it in natural order (which is shown below). I don't know how to print in reverse order. I know a way to use a comparator, but I'm a little rusty so I've done what I can. Furthermore, I don't know how to set the comparator up to sort the Treemap into reverse order.

Here's my method to print Unsorted and Naturally sorted:

private static void sortPrintFrequencies(Map<String,Integer> vocabulary, PrintStream                                                  output {
Iterator iterator = vocabulary.keySet().iterator();
System.out.println("Unsorted");

while (iterator.hasNext()) {
 String key = iterator.next().toString();
 String value = vocabulary.get(key).toString();
 String times = "times.";
 String appears = "appears";

System.out.printf("%35s", key + "    " + appears + "    " + value + " "+ times);
System.out.println();
    }
System.out.println("========================================");
System.out.println("SORTED NATURALLY BY KEY");
TreeMap newVocabulary = new TreeMap(vocabulary);
Iterator iterator2 = newVocabulary.keySet().iterator();
while (iterator2.hasNext()) {
  String key = iterator2.next().toString();
  String value = newVocabulary.get(key).toString();
  String times = "times.";
  String appears = "appears";

    System.out.printf("%35s", key + "    " + appears + "    " + value + " "+ times);
    System.out.println();
}
  TreeMap revVocabulary = new TreeMap(new RevCmpKey());

  System.out.println("========================================");

}

Here's my comparator:

import java.util.*;
public class RevCmpKey implements Comparator<String> {
public int compare(String e1, String e2) {
    //compareTo in String classs
    if(e1.compareTo(e2) <1)return -1;
    if(e1.compareTo(e2) >1)return 1;
    return 0;
}
}
like image 741
IC2D Avatar asked Feb 18 '12 03:02

IC2D


People also ask

Does TreeMap sort automatically?

2. Default Sorting in TreeMap. By default, TreeMap sorts all its entries according to their natural ordering. For an integer, this would mean ascending order and for strings, alphabetical order.

Does TreeMap preserve order?

A TreeMap is a Map that maintains its entries in ascending order, sorted according to the keys' natural ordering, or according to a Comparator provided at the time of the TreeMap constructor argument. The TreeMap class is efficient for traversing the keys in a sorted order.


2 Answers

What about copying your Map into a new one naturally reverse ordered?

new TreeMap<String,Integer>(Collections.reverseOrder())
like image 167
Olivier C Avatar answered Sep 24 '22 14:09

Olivier C


Short Answer:

Use descendingKeySet or descendingMap.

Long Answer:

Solution 1:

As Oliver correctly mentioned, you can copy the map into a new TreeMap to reach your goal.

However, when using descendingKeySet, you won't need to create a new TreeMap:

treeMap.descendingKeySet()

Here's an example:

private static void printReverseTreeMap(TreeMap<String,Integer> treeMap){
    for(String key : treeMap.descendingKeySet()){
        System.out.println("value of " + key + " is " + treeMap.get(key));
    }
}

Solution 2:

You can also create a new Map in reverse order using descendingMap as well as Collections.reverseOrder():

NavigableMap<String, Integer> reveresedTreeMap = treeMap.descendingMap();

Note that descendingMap returns NavigableMap.

like image 21
Positive Navid Avatar answered Sep 22 '22 14:09

Positive Navid