繁体   English   中英

检查对象列表是否包含Java中的另一个对象

[英]Check if a list of objects contains another object in Java

为什么ts.contains(t)返回false ,我该如何解决? 看一下我的代码,请:

class MyList {
    private String x;

    public MyList (String x) {
        this .x = x;
    }

    public String toString () {
        return x;
    }   

    public static void main ( String [] args ) {
        List<MyList> ts = new ArrayList<MyList>();
        ts.add (new MyList ("one"));
        ts.add (new MyList ("two"));
        ts.add (new MyList ("three"));

        MyList t = new MyList("one");
        System.out.println ("Is t in ts? " + ts.contains(t));
    }
}

谢谢大家的帮助。 SamzSakerz和michaeak答案都可以正常工作。

只需实现equals()方法:

class MyList {
    private String x;

    public MyList (String x) {
        this .x = x;
    }

    @Override
    public String toString () {
        return x;
    }   


    public static void main ( String [] args ) {
        List<MyList> ts = new ArrayList<MyList>();
        ts.add (new MyList ("one"));
        ts.add (new MyList ("two"));
        ts.add (new MyList ("three"));

        MyList t = new MyList("one");
        System.out.println ("Is t in ts? " + ts.contains(t));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((x == null) ? 0 : x.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        MyList other = (MyList) obj;
        if (x == null) {
            if (other.x != null) {
                return false;
            }
        } else if (!x.equals(other.x)) {
            return false;
        }
        return true;
    }
}

输出Is t in ts? true Is t in ts? true

为类Object定义了equals()方法,该类是每个类的顶级类。 contains()方法以合同方式检查所请求的对象a是否包含在列表中(即,同一对象包含在列表中)或相等的对象b(即aequals(b)为true)包含在列表中。

对于List.contains(obj) ,不需要实现hashCode方法,但是,无论何时实现equals()并确保两个方法都依赖于相同的属性,建议实现hashCode()

您必须重写equalshashCode方法。

contains依赖于equals ,而equals的默认实现是对其身份进行比较。 然后,如果equals是完全相同的对象,则equals仅返回true。

为了实现equals方法,您必须确定何时将两个对象视为相等。 在您的情况下,我假设如果对象的唯一字段s与另一个字段相等,那么您希望将它们视为相等。

更多:

您可以使用以下命令检查列表中是否包含具有特定属性的对象

        System.out.println("Is t in ts? " + ts.stream().anyMatch(x -> x.x.equals("one")));

就像其他人指出的那样,您需要重写equalshashcode我们可以在1行中完成此操作。

@Override
public int hashCode() {
    return toString().hashCode();
}

@Override
public boolean equals(Object obj) {
    return this == obj || obj != null && getClass() == obj.getClass() && toString().equals(obj.toString());
}

现在我们得到的输出是

Is t in ts? true

这是完整的代码:

import java.util.ArrayList;
import java.util.List;

class MyList {
    private String x;

    public MyList(String x) {
        this.x = x;
    }

    public static void main(String[] args) {
        List<MyList> ts = new ArrayList<MyList>();
        ts.add(new MyList("one"));
        ts.add(new MyList("two"));
        ts.add(new MyList("three"));

        MyList t = new MyList("one");
        System.out.println("Is t in ts? " + ts.contains(t));
    }

    @Override
    public String toString() {
        return x;
    }

    @Override
    public int hashCode() {
        return toString().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return this == obj || obj != null && getClass() == obj.getClass() && toString().equals(obj.toString());
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM