我需要为 ConcurrentSkipListMap 定义一个自定义比较器,我使用此代码根据“LogicalClock”进行排序,但结果与我预期的不符。我创建这样的密钥:“ClientId”+“:”+“LogicalClock”
class Qentry{
int AckCount;
int ClientID;
long LogicalClock;
}
Comparator<String> LogicalClockComparator = new Comparator<String>() {
@Override public int compare(String k1, String k2) {
if (k1.compareTo(k2)==0)
return 0;
return (int)( Long.valueOf(k1.substring(k1.indexOf(":")+1)) -Long.valueOf(k2.substring(k1.indexOf(":")+1) ));
}
ConcurrentSkipListMap<String,Qentry> q;
q =new ConcurrentSkipListMap<String,Qentry>(LogicalClockComparator);