如何以相反的顺序打印树形图

时间:2012-02-18 03:18:19

标签: java treemap

在我的作业中,我们从文件中读取文字:

  

成为或不成为:这就是问题:
   是否在心中更高尚受苦

然后计算每次发生的次数。我已经能够打印这个未分类的地图,然后我能够制作一个TreeMap并以自然顺序打印它(如下所示)。我不知道如何以相反的顺序打印。我知道一种使用比较器的方法,但我有点生疏,所以我尽我所能。此外,我不知道如何设置比较器以将Treemap排序为相反的顺序。

这是我打印未排序和自然排序的方法:

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("========================================");

}

这是我的比较器:

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;
}
}

5 个答案:

答案 0 :(得分:59)

如何将地图复制到一个自然反向排序的新地图中?

new TreeMap<String,Integer>(Collections.reverseOrder())

答案 1 :(得分:5)

简答:

使用descendingKeySetdescendingMap

长答案:

解决方案1:

正如Oliver正确mentioned,您可以将地图复制到新的TreeMap中以达到目标。

但是,使用descendingKeySet时,您不需要创建新的TreeMap:

treeMap.descendingKeySet()

以下是一个例子:

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

解决方案2:

您还可以使用descendingMap以及Collections.reverseOrder()以相反的顺序创建新地图:

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

请注意,descendingMap会返回NavigableMap

答案 2 :(得分:1)

由于String已经具有可比性,因此反比较器非常简单:

public class RevCmpKey implements Comparator<String> {

  public int compare(String e1, String e2) {
    return - e1.compareTo(e2);
  }
}

另一个问题是你没有指定泛型的值;构造TreeMap时,应使用

TreeMap<String, Integer> revVocabulary = new TreeMap<String, Integer>(new RevCmpKey());

然后你只需要调用putAll即可。

答案 3 :(得分:1)

在这里,您还可以准备ReverseComparator并用于Ordered-Collection中使用的任何类:

class ReverseComparator implements Comparator<Comparable<Object>> {

    @Override
    public int compare(Comparable<Object> o1, Comparable<Object> o2) {

        return o2.compareTo( o1 );
    }

}

通常我们将o1与o2进行比较,但反向比较o2与o1

答案 4 :(得分:1)

只需尝试以下

private TreeMap<BigInteger, List<TicketingDocumentServiceCouponHistory>> getCpnHistoryMap(
        List<TicketingDocumentHistory> tktHistoryList,List<TicketingDocumentServiceCouponTicket> couponList){

    TreeMap<BigInteger, List<TicketingDocumentServiceCouponHistory>> cpnHistoryMap = new TreeMap<>(Collections.reverseOrder());
    cpnHistoryMap.put(BigInteger.valueOf(Integer.MAX_VALUE), getOcCpnHistoryList(couponList));
    tktHistoryList
            .stream()
            .filter(history -> history.getCode().equals(RVL))
            .forEach(history -> cpnHistoryMap.put(history.getSequence(), getCpnHistoryList(cpnHistoryMap, history)));

    TreeMap<BigInteger, List<TicketingDocumentServiceCouponHistory>> cpnHistMapInOrder =  new TreeMap<>();
    cpnHistMapInOrder.putAll(cpnHistoryMap);
    return cpnHistMapInOrder;
}