Hazelcast Java-Predicates.and忽略除第一个谓词以外的谓词
我有一个Hazelcast Java-Predicates.and忽略除第一个谓词以外的谓词,java,hazelcast,Java,Hazelcast,我有一个IMap,其中有一个类作为键。此键有两个属性: static class MapKey implements Serializable{ String uid; String service; public MapKey() { } public MapKey(String uid, String service) { this.uid = uid; th
IMap
,其中有一个类作为键。此键有两个属性:
static class MapKey implements Serializable{
String uid;
String service;
public MapKey() {
}
public MapKey(String uid, String service) {
this.uid = uid;
this.service = service;
}
public String getUid() {
return uid;
}
...
}
我只是用两个简单的值初始化贴图:
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
final IMap<MapKey, String> map = hz.getMap("testmap");
map.addIndex("__key#uid", false);
map.put(new MapKey("uid1","service1"),"value1");
map.put(new MapKey("uid1","service2"),"value2");
hazelcastance hz=Hazelcast.newhazelcastance();
最终IMap映射=hz.getMap(“testmap”);
map.addIndex(“\uu key\uid”,false);
地图放置(新地图键(“uid1”、“服务1”)、“值1”);
地图放置(新地图键(“uid1”、“服务2”)、“值2”);
然后我用ands构建一个谓词:
static Predicate<MapKey, String> buildPredicate(MapKey key){
final EntryObject entryObject = new PredicateBuilder().getEntryObject().key();
final List<Predicate<MapKey, String>> predicateList = new ArrayList<>();
predicateList.add(entryObject.get("uid").equal(key.getUid()));
predicateList.add(entryObject.get("service").equal(key.getService()));
final com.hazelcast.query.Predicate predicate = Predicates.and(predicateList.toArray(new Predicate[predicateList.size()]));
return predicate;
}
静态谓词buildPredicate(MapKey){
final EntryObject EntryObject=new PredicateBuilder().getEntryObject().key();
最终列表谓词列表=新的ArrayList();
add(entryObject.get(“uid”).equal(key.getUid());
add(entryObject.get(“service”).equal(key.getService());
final com.hazelcast.query.Predicate Predicate=Predicates.and(predicateList.toArray(新谓词[predicateList.size()]);
返回谓词;
}
当我使用这个谓词时,它只返回按uid过滤的键,这意味着集合值的大小是2,而不是预期的1
Predicate<MapKey, String> predicate = buildPredicate(new MapKey("uid1","service1"));
Collection<MapKey> values = map.keySet(predicate);
Predicate-Predicate=buildPredicate(新映射键(“uid1”、“service1”);
集合值=map.keySet(谓词);
有人能解释我的这种行为吗?我缺少的是什么?问题是,您在
entryObject上操作时,它是否是不可变的对象,但它不是不可变的。它实际上是一个生成器,操作entryObject.get(“uid”).equal(key.getUid())
会更改其状态并记录谓词条件。这就是为什么在两个完全相等的谓词上执行谓词和()
试试下面的方法,它会达到你想要达到的目的
static Predicate<MapKey, String> buildPredicate(MapKey key){
final List<Predicate<MapKey, String>> predicateList = new ArrayList<>();
predicateList.add(new PredicateBuilder().getEntryObject().key().get("uid").equal(key.getUid()));
predicateList.add(new PredicateBuilder().getEntryObject().key().get("service").equal(key.getService()));
final com.hazelcast.query.Predicate predicate = Predicates.and(predicateList.toArray(new Predicate[predicateList.size()]));
return predicate;
}
静态谓词buildPredicate(MapKey){
最终列表谓词列表=新的ArrayList();
add(new PredicateBuilder().getEntryObject().key().get(“uid”).equal(key.getUid());
add(new PredicateBuilder().getEntryObject().key().get(“service”).equal(key.getService());
final com.hazelcast.query.Predicate Predicate=Predicates.and(predicateList.toArray(新谓词[predicateList.size()]);
返回谓词;
}