Jür*_* K. 3 java sorting performance tuples list
给定的通用数据类型看起来像这样:HashMap<EdgeTuple, Double> edgeList其中元组是一个类EdgeTuple而Double是一个对任务不重要的权重:
class EdgeTuple{
int label1;
int label2;
public EdgeTuple(int label1, int label2){
int min = Math.min(label1, label2);
int max = Math.max(label1, label2);
this.label1 = min;
this.label2 = max;
}
}
Run Code Online (Sandbox Code Playgroud)
因此,您可以看到元组已经在第一个位置上具有较小的值.我想要做的是对最终输入顺序应该如下所示的列表进行排序:
条目0:[(0,某事物); some_weight]
第1项:[(1,某事); some_weight]
...
条目n-1:[(last_value,something); some_weight]
所以基本上我需要做的是对元组的第一个值进行升序排序.我对这个主题的红色最喜欢的现有答案,但仍然找不到令人满意的东西.
一种可能的解决方案是依靠比较器,如下所示:
Comparator<Tuple> myComparator = new Comparator<Tuple>() {
public int compare(Tuple t1, Tuple t2) {
//the comparison rules go here
}
};
Collections.sort(tupleList, myComparator);
Run Code Online (Sandbox Code Playgroud)
每对元组的比较似乎并不安静.所以我的问题是,您是否知道其他任何排序方式?也许一些新的数据类型为给定的任务提供了一个合适的更高性能的接口?
谢谢
你可以Comparable在你的实现界面EdgeTuple:
public static class EdgeTuple implements Comparable<EdgeTuple> {
int label1;
int label2;
public EdgeTuple(int label1, int label2){
int min = Math.min(label1, label2);
int max = Math.max(label1, label2);
this.label1 = min;
this.label2 = max;
}
@Override
public int compareTo(EdgeTuple o) {
return this.label1 - o.label1;
}
}
Run Code Online (Sandbox Code Playgroud)
并用于TreeMap存储元组的预分类映射,不要每次都对它进行排序.
| 归档时间: |
|
| 查看次数: |
2401 次 |
| 最近记录: |