Java cqengine铁路超高指数等于

Java cqengine铁路超高指数等于,java,cqengine,Java,Cqengine,我试图添加一个索引,其中我的覆盖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; } publ

我试图添加一个索引,其中我的覆盖
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 + '}';
        }

}
public static final ResultSet<Car> get(final IndexedCollection<Car> indexedCollection, final Car car) {
    return indexedCollection.retrieve(QueryFactory.equal(Car.CAR, car));
}
public static void main(String args[]) {
            IndexedCollection<Car> cars = new ConcurrentIndexedCollection<>();
            cars.addIndex(NavigableIndex.onAttribute(Car.CAR));
}
公共静态类车{
最终字符串id;
私有字符串名称;
公共车辆(字符串id、字符串名称){
this.id=id;
this.name=名称;
}
公共字符串getId(){
返回id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共静态最终属性CAR=新的SimpleAttribute(){
@凌驾
公共汽车getValue(汽车、查询选项查询选项){
返回车;
}
};
@凌驾
公共字符串toString(){
返回“Car{”+“id=“+id+”,name=“+name+”}”;
}
}
Fetcher.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 + '}';
        }

}
public static final ResultSet<Car> get(final IndexedCollection<Car> indexedCollection, final Car car) {
    return indexedCollection.retrieve(QueryFactory.equal(Car.CAR, car));
}
public static void main(String args[]) {
            IndexedCollection<Car> cars = new ConcurrentIndexedCollection<>();
            cars.addIndex(NavigableIndex.onAttribute(Car.CAR));
}
公共静态最终结果集get(最终索引集合IndexedCollection,最终汽车){
return indexedCollection.retrieve(QueryFactory.equal(Car.Car,Car));
}
Main.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 + '}';
        }

}
public static final ResultSet<Car> get(final IndexedCollection<Car> indexedCollection, final Car car) {
    return indexedCollection.retrieve(QueryFactory.equal(Car.CAR, car));
}
public static void main(String args[]) {
            IndexedCollection<Car> cars = new ConcurrentIndexedCollection<>();
            cars.addIndex(NavigableIndex.onAttribute(Car.CAR));
}
publicstaticvoidmain(字符串参数[]){
IndexedCollection cars=新的ConcurrentIndexedCollection();
cars.addIndex(NavigableIndex.onAttribute(Car.Car));
}
问题在这一行
cars.addIndex(NavigableIndex.onAttribute(Car.Car))其中错误消息为
找不到适用于onAttribute(属性)的方法
。我是做错了什么,还是应该使用另一个调用来代替?

删除
cars.addIndex(NavigableIndex.onAttribute(Car.Car)),因为它不是一个真正有用的索引。。。我认为这不是开发者的动机。您应该为
CAR\u ID
CAR\u 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 + '}';
    }
}
等级车{
私有最终int id;
私有字符串名称;
公共汽车(整数i,字符串名称){
这个id=i;
this.name=名称;
}
公共int getId(){
返回id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}    
@凌驾
公共布尔等于(对象obj){
如果(this==obj)返回true;
if(obj==null)返回false;
如果(!(车载obj实例))返回false;
汽车=(汽车)obj;
if(car.getId()==this.getId())
if(car.getName().equals(this.getName()))
返回true;
返回false;
}
公共静态最终属性CAR=新的SimpleAttribute(){
@凌驾
公共汽车getValue(汽车、查询选项查询选项){
返回车;
}
};
@凌驾
公共字符串toString(){
返回“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()));
IndexedCollection cars=新的ConcurrentIndexedCollection();
添加(新车(1,“测试”);
添加(新车(2,“测试2”);
添加(新车(3,“测试3”);
汽车s=新车(2,“测试2”);
结果集cs=cars.retrieve(QueryFactory.equal(Car.Car,s));
cs.forEach(c->System.out.println(c.toString());

索引收集车
需要一辆
,但
属性车
不是一辆车。这有点像简单的尝试。尝试调用
getValue()
@asdf:我在哪里调用
getValue()
完全正确?参见此示例:@asdf:I实际上没有看到在任何地方使用
equals()
?您应该首先解决另一个问题。。。