gpt4 book ai didi

java - cqengine 无法按 equals 索引

转载 作者:行者123 更新时间:2023-11-30 07:04:45 24 4
gpt4 key购买 nike

我正在尝试添加一个索引,其中我的覆盖 equals()确定两个对象是否相同。

Car.java

public static class Car {

final String id;
private String name;

public Car(String id, String name) {
this.id = id;
this.name = name;
}

public String getId() {
return id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public static final Attribute<Car, Car> CAR = new SimpleAttribute<Car, Car>() {
@Override
public Car getValue(Car car, QueryOptions queryOptions) {
return car;
}
};

@Override
public String toString() {
return "Car{" + "id=" + id + ", name=" + name + '}';
}

}

Fetcher.java

public static final ResultSet<Car> get(final IndexedCollection<Car> indexedCollection, final Car car) {
return indexedCollection.retrieve(QueryFactory.equal(Car.CAR, car));
}

Main.java

public static void main(String args[]) {
IndexedCollection<Car> cars = new ConcurrentIndexedCollection<>();
cars.addIndex(NavigableIndex.onAttribute(Car.CAR));
}

问题出在这一行 cars.addIndex(NavigableIndex.onAttribute(Car.CAR));其中错误消息为 no suitable method found for onAttribute(Attribute<Car,Car>) 。我在这里做错了什么,还是应该使用另一个调用?

最佳答案

删除cars.addIndex(NavigableIndex.onAttribute(Car.CAR));,因为它并不是真正有用的索引......而且我认为这不是开发人员的动机。您应该为 CAR_IDCAR_NAME 创建属性,并创建一个查询以进行比较。在这种情况下,我滥用(以实现您所期望的)IndexedCollection 作为简单的Set。但是...如果我理解正确的话,这是一个可能的解决方案:

覆盖汽车中的等于:

class Car {
private final int id;
private String name;
public Car(int i, String name) {
this.id = i;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null) return false;
if (!(obj instanceof Car)) return false;
Car car = (Car) obj;
if(car.getId() == this.getId())
if(car.getName().equals(this.getName()))
return true;
return false;
}
public static final Attribute<Car, Car> CAR = new SimpleAttribute<Car, Car>() {
@Override
public Car getValue(Car car, QueryOptions queryOptions) {
return car;
}
};
@Override
public String toString() {
return "Car{" + "id=" + id + ", name=" + name + '}';
}
}

主要:

IndexedCollection<Car> cars = new ConcurrentIndexedCollection<>();
cars.add(new Car(1, "test"));
cars.add(new Car(2, "test2"));
cars.add(new Car(3, "test3"));
Car s = new Car(2, "test2");
ResultSet<Car> cs= cars.retrieve(QueryFactory.equal(Car.CAR, s));
cs.forEach(c -> System.out.println(c.toString()));

关于java - cqengine 无法按 equals 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40333646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com