简体   繁体   English

尝试使用.contains()Java方法

[英]Attempting to use .contains() Java method

This is my "main" code: 这是我的“主要”代码:

ArrayList<Person> people = new ArrayList<Person>();

  people.add(new Person("Aaron Morgan", 31));
  people.add(new Person("Faye Palmer", 27));
  people.add(new Person("Dexter Koh", 33));
  people.add(new Person("Sallie Axl", 25));

  if (people.contains(new Person("Adam Kelly", 48)))
  {
     System.out.print("MATCH FOUND");
  }

    else
  {
     System.out.print("MATCH NOT FOUND");
  }

I'm attempting to create a method which will compare "Adam Kelly" with the list of people in the array list. 我正在尝试创建一种将“亚当·凯利”与数组列表中的人员列表进行比较的方法。 I can take in the "Adam Kelly" into the method fine, however I'm not sure how I'm meant to compare it in the class/method. 我可以将“ Adam Kelly”引入方法中,但是我不确定在类/方法中如何比较它。

Can anyone shed any light on this? 谁能对此有所启示?

Implement .equals() like this in your Person class: Person类中实现.equals()

@Override
public boolean equals(Object o) {
 if(o == null) { return false; }
 if(!(o instanceof Person)) { return false; }

 Person person = (Person) o;
 return this.name.equals(person.getName()) && this.age == person.getAge();
}

The .contains() method will look for an existing value based on their .equals() representation. .contains()方法将根据其.equals()表示形式查找现有值。 The standard implementation for a reference type is simply the address location of the object in memory. 引用类型的标准实现只是对象在内存中的地址位置。 Since you have two different objects this will return a different address and thus it won't be found. 由于您有两个不同的对象,这将返回一个不同的地址,因此将找不到该地址。

What you have to do is create your own implementation that uses the fields you want. 您要做的是使用所需字段创建自己的实现。

Constains depends on the Object's equals() implementation. 包含取决于对象的equals()实现。 So you need to write an equal implementation to Person(). 因此,您需要为Person()写一个相等的实现。 Either way contains() on list is not very optimal and has O(n) runtime. 无论哪种方式,列表中的contains()都不是很理想,并且具有O(n)运行时。 It's much better to use a Hash based collection, namely Set() and make sure Person has both valid hashCode() and equals() implementation. 最好使用基于哈希的集合,即Set()并确保Person具有有效的hashCode()和equals()实现。

In your People class you need to implement an equals method. 在People类中,您需要实现一个equals方法。

public boolean equals(Object obj) {
  if (obj instanceof Person) {
    Person p = (Person) obj;
    return ((this == obj) || (this.getName().equals(p.getName()) && 
           this.getAge() == p.getAge()));
  }
  return false;
}

必须编写自定义的equals方法,在其中比较属性

equals(Object obj) 

Basically you need to implement object equality by using Java's equals and hashcode in the Object class. 基本上,您需要通过在Object类中使用Java的equals哈希码来实现对象相等。 Otherwise it will use the Object class implementation that only checks references by using the == operator. 否则,它将使用仅使用==运算符检查引用的Object类实现。

Something like this in your class Person: 您班上的人是这样的:

class Person {

    // your fields and stuff

    @Override
    public boolean Equals(Object o) {
        if (o == null) {
            return false;
        }
        if (!(o instanceof Person)) {
            return false;
        }

        Person that = (Person) o;

            if(this.name != null){
                return (this.name.equals(that.name)) && (this.age == that.age);
            }

            return false;
    }

    @Override
    public int hashCode(){
          // use a library here!
    }

} 

There's an easy way to make that. 有一种简单的方法可以做到这一点。 First you have to create an iterator for your ArrayList. 首先,您必须为ArrayList创建一个迭代器。

Iterator it = people.iterator();

then explore all elements and compare 然后探索所有元素并进行比较

while ( it.hasNext() ) {
  Object obj = it.next();
  Person person= (Person)obj;
  if(person.getName().equals("Adam Kelly") && person.getAge().equals("43"))
  {
   System.out.print("MATCH FOUND");
   }
else{
   System.out.print("MATCH NOT FOUND");
  }
}

hope this solve your problem. 希望这能解决您的问题。

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

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