cqengine不能按相等索引
cqengine cant index by equals
我正在尝试添加一个索引,我的覆盖 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_ID
和 CAR_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()));
我正在尝试添加一个索引,我的覆盖 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_ID
和 CAR_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()));