一、利用TreeMap做排序,代码如下:
java.util.SortedMap<Double, scala.Tuple2<String,String>> topN = new java.util.TreeMap<Double, scala.Tuple2<String,String>>();
topN.put(-0.223,new scala.Tuple2<String,String>("1","1"));
topN.put(-0.5239,new scala.Tuple2<String,String>("2","1"));
topN.put(-0.323,new scala.Tuple2<String,String>("3","1"));
topN.put(-0.423,new scala.Tuple2<String,String>("4","1"));
topN.put(-0.523,new scala.Tuple2<String,String>("5","1"));
topN.put(-0.623,new scala.Tuple2<String,String>("6","1"));
for (Map.Entry<Double, scala.Tuple2<String,String>> entry : topN.entrySet()) {
System.out.println(entry.getValue()._1);
}
结果发现,排序输出没有问题,都是从小到大输出;但是如果存在排序值相同的数据时,就会出现数据覆盖。
二、利用Collections.sort 做排序,则不会出现map中的覆盖问题,代码如下:
List<Tuple2<Double,Tuple2<String,String>>> listA = new ArrayList<Tuple2<Double,Tuple2<String,String>>>();
listA.add(new Tuple2<Double,Tuple2<String,String>>(-0.223,new scala.Tuple2<String,String>("1","1")));
listA.add(new Tuple2<Double,Tuple2<String,String>>(-0.5239,new scala.Tuple2<String,String>("2","1")));
listA.add(new Tuple2<Double,Tuple2<String,String>>(-0.323,new scala.Tuple2<String,String>("3","1")));
listA.add(new Tuple2<Double,Tuple2<String,String>>(-0.423,new scala.Tuple2<String,String>("4","1")));
listA.add(new Tuple2<Double,Tuple2<String,String>>(-0.523,new scala.Tuple2<String,String>("5","1")));
listA.add(new Tuple2<Double,Tuple2<String,String>>(-0.623,new scala.Tuple2<String,String>("6","1")));
Collections.sort(listA, new Comparator<Tuple2<Double,Tuple2<String,String>>>() {
public int compare(Tuple2<Double,Tuple2<String,String>> arg0, Tuple2<Double,Tuple2<String,String>> arg1) {
Double d0=arg0._1;
Double d1=arg1._1;
return d0.compareTo(d1);
}
});
for (Tuple2<Double,Tuple2<String,String>> p : listA) {
System.out.println(p._2._1);
}