1

how to get a specific java object from java.util.List<Object> using equals method exemple :

  Class Person{
       String name;
       int age;
       //...

       @Override
       public boolean equals(String str) {
            return (this.name.equals(str.name));
       }
    }

main{
   List<Person> list = personDao.findAll();

   if(list.contain("joe")){
      // how to get the Person named joe ????
   }
}

If you want to get a reference to the specific Person in the List:

Person foo = null;
for (Person p : list) {
    if (p.getName().equals("joe")) {
        foo = p;
        break;
    }
}
// now foo is your person (if he exists) or null (if he doesnt exist);

This will only find the first Person named joe.

4

6 に答える 6

3

リスト内の特定の人物への参照を取得する場合:

Person foo = null;
for (Person p : list) {
    if (p.getName().equals("joe")) {
        foo = p;
        break;
    }
}
// now foo is your person (if he exists) or null (if he doesnt exist);

これにより、joe という名前の最初の Person のみが検索されます。

于 2012-11-06T17:15:31.533 に答える
2

Person.equals()メソッドの署名が正しくありません...引数のタイプは、次のように常に「オブジェクト」である必要があります。

@Override
public boolean equals(Object obj) {
    return (obj instanceof Person) && ((Person) obj).name.equals(this.name);
}

次に、list.contains(new Person( "name")を使用するというjlordolessの提案が機能します。

于 2012-11-06T17:18:49.130 に答える
2

Simply iterate. There's no method on the List interface that returns an object from the list.

Person joe = null;
List<Person> persons = personDao.findAll();
for (Person thisPerson : persons) {
  if ("joe".equals(thisPerson.getName())) {
     joe = thisPerson;
     break;
  }
}

This will set Person joe to the first person named joe. If you're looking for the last one in the collection, remove the break statement.

于 2012-11-06T17:21:07.003 に答える
1

I think that you have a design flaw here. What happens if there are 2 joe-s?

If you don't have duplicate names you can create a getter for the name in Person:

public String getName() {
    return name;
}

and after that you can do this:

Person joe = null;
for(Person person : list) {
    if("joe".equals(person.getName()) {
        joe = person;
        break;
    }
}

This way you avoid creating unnecessary instances of Person.

于 2012-11-06T17:20:37.660 に答える
1

You should not use equals like that. Overriding equals is somewhat complex, you have to verride hashcode too, for example. A list-based solution with minimal changes:

Class Person{
   String name;
   int age;
   //...

   public String getName() {
        return name;
   }
}

main {
   List<Person> list = personDao.findAll();

   Person foundPerson = null;
   for(Person person : list) {
       if (person.getName().equals("joe")) {
           foundPerson = person;
           break;
       }
   }

   if (foundPerson != null) {
       // do something
   }
}

However, I would consider using a Map<String, Person>, with name as key. Overriding equals in Person while ignoring age does not sound very good, really.

于 2012-11-06T17:24:42.000 に答える
0

@ハイド:あなたの答えに答えることはできません。「hashCode」のオーバーライドは、ハッシュが使用されている場合にのみ必要です。HashMapまたはHashing関連の他のAPIを使用したい場合は、hashCodeとequalsをオーバーライドする必要があります...それ以外の場合は「equals」で問題なく動作します。

于 2012-11-06T18:13:19.963 に答える