首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cqengine不能按等于进行索引

cqengine不能按等于进行索引
EN

Stack Overflow用户
提问于 2016-10-30 21:09:32
回答 1查看 260关注 0票数 2

我试图添加一个索引,在该索引中,我的重写equals()确定两个对象是否相同。

Car.java

代码语言:javascript
复制
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

代码语言:javascript
复制
public static final ResultSet<Car> get(final IndexedCollection<Car> indexedCollection, final Car car) {
    return indexedCollection.retrieve(QueryFactory.equal(Car.CAR, car));
}

Main.java

代码语言:javascript
复制
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>)。我在这里做错了什么吗?还是我应该用另一个电话来代替?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-31 00:30:32

删除cars.addIndex(NavigableIndex.onAttribute(Car.CAR));,因为它实际上不是一个有用的索引..。我认为这不是开发商的动机。您应该为CAR_IDCAR_NAME创建属性,并创建一个查询以进行比较。在这种情况下,我误用(为了实现您预期的) IndexedCollection作为一个简单的Set。但是..。如果我对你的理解是正确的,这里有一个可能的解决办法:

覆盖在汽车中等于:

代码语言:javascript
复制
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 + '}';
    }
}

Main:

代码语言:javascript
复制
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()));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40333646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档