List <mymodel> .contains(MyModel)java无法正常工作

时间:2019-03-02 07:10:27

标签: java arraylist contains

我的MyModel类包含:

private int value;

public int getValue() {
    return value;
}

public void setValue(int value) {
    this.value = value;
}

现在我已填充MyModel类:

private final List<MyModel> listModel = new ArrayList<>();
private Mymodel myModel;

 myModel= new MyModel ();
 myModel.setValue(1);
 myModel.setValue(2);
 myModel.setValue(3);
 listModel.add(myModel);

我想确定listModel是否包含myModel

listModel.contains(myModel)

我进行了一些搜索,我知道包含使用了等式,但是我正在寻找找到解决方案的正确方法。 预先感谢

3 个答案:

答案 0 :(得分:2)

如果您查看contains类中ArrayList的实现,您会发现它在内部使用equals方法来找到对象:

public boolean contains(Object o) {
    return indexOf(o) >= 0;
}

public int indexOf(Object o) {
    return indexOfRange(o, 0, size);
}

int indexOfRange(Object o, int start, int end) {
    Object[] es = elementData;
    if (o == null) {
       ...
    } else {
        for (int i = start; i < end; i++) {
            if (o.equals(es[i])) { //calling overridden equals of the object instance, else it will revert to the Object's equals which simply compares the reference 
                return i;
            }
        }
    }
    return -1;
}

因此,您需要为equals类创建hashCodeMyModel。您可以借助正在使用的IDE来为您生成IDE:

例如:

class MyModel{
    private int value;

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof MyModel)) return false;
        MyModel myModel = (MyModel) o;
        return value == myModel.value;
    }

    @Override
    public int hashCode() {
        return Objects.hash(value);
    }
}

答案 1 :(得分:1)

检查包含的唯一方法是覆盖equals()方法。如果您查看如何在ArrayList中实现contains方法,则可以轻松找到它。 contains()方法将调用indexOf()方法,该方法将返回列表中元素的索引。如果索引大于0,则其contains()方法将返回true。

让我们看看如何实现indexOf()方法。

public int indexOf(Object o) {
    if (o == null) {
        for (int i = 0; i < size; i++)
            if (elementData[i]==null)
                return i;
    } else {
        for (int i = 0; i < size; i++)
            if (o.equals(elementData[i]))
                return i;
    }
    return -1;
}

这意味着indexOf()方法只是遍历列表中的元素并在equals()类中调用MyModel方法。

因此,如果不从Object类中覆盖equals()方法,就没有其他方法可以检查包含内容

答案 2 :(得分:0)

尝试运行以下代码,我已经对其进行了修改并添加了日志,现在您可以检查控制台上的输出了: 我建议您使用equals()检查对象。

class MyModel {
private int value;

public int getValue() {
    return value;
}

public void setValue(int value) {
    this.value = value;
}

@Override
public String toString() {
    return "MyModel{" +
            "value=" + value +
            '}';
}

}

final List<MyModel> listModel = new ArrayList<>();
    MyModel myModel;

    myModel= new MyModel();
    myModel.setValue(1);
    myModel.setValue(2);
    myModel.setValue(3);
    listModel.add(myModel);
    //here your myModel object has '3' value and list has [3], So this will return `true`
    System.out.println("myModel=>"+myModel.toString()+"-->List = "+listModel.toString()+",, CONTAINS. =>"+listModel.contains(myModel));

    myModel= new MyModel();
    myModel.setValue(8);
    listModel.add(myModel);
    //here your myModel object has '8' value and list has [3,8], So this will return `true`
    System.out.println("myModel=>"+myModel.toString()+"-->List = "+listModel.toString()+",, CONTAINS. =>"+listModel.contains(myModel));

    myModel= new MyModel();
    myModel.setValue(100);
    //here your myModel object has '100' value and list has [3,8], So this will return `false`
    System.out.println("myModel=>"+myModel.toString()+"-->List = "+listModel.toString()+",, CONTAINS. =>"+listModel.contains(myModel));