ConcurrentSkipListMap和多列比较器

时间:2013-03-25 07:37:53

标签: java

我有以下课程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()) {
...
}

OR

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()) {
...
}

1 个答案:

答案 0 :(得分:0)

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

Ajay