0

我有以下课程KeyObj

public class KeyObject {
    private byte[] P1;
    private byte[] P2;
    private byte[] P3;
}

我想将此类的对象放入ConcurrentSkipListMap我对如何编写比较器感到困惑,以便可以进行范围查询。即如果P1给定那么它返回我p2 and p3。像下面的东西

KeyObject fromKey = new KeyObject(K1.P1, P2_MIN_VALUE, P3_MIN_VALUE);
KeyObject toKey = new KeyObject(K2.P1, P2_MAX_VALUE, P3_MAX_VALUE);
ConcurrentNavigableMap<KeyObject, Object> subview = table.submap(fromkey, toKey);
for (ConcurrentSkipListMap.Entry<KeyObject, Object> entry : subView.entrySet()) {
...
}

或者

KeyObject fromKey = new KeyObject(K1.P1, K1.P2, P3_MIN_VALUE);
KeyObject toKey = new KeyObject(K2.P2, K2.P2, P3_MAX_VALUE);
ConcurrentNavigableMap<KeyObject, Object> subview = table.submap(fromkey, toKey);
for (ConcurrentSkipListMap.Entry<KeyObject, Object> entry : subView.entrySet()) {
...
}
4

1 回答 1

0

使用可比较或比较器接口,并以希望它们行为的方式在其中定义方法 compareto 或 compare。

阿杰

于 2013-03-25T08:38:20.430 回答