如何使用 indexOf() 函数查找具有特定 属性 的对象

How can I use the indexOf() function to find an object with a certain property

我有一个对象,宠物,其中一个功能是检索它的名字。

public class pet{
    private String petName;
    private int petAge;

    public pet(String name, int age){
        petName = name;
        petAge = age;
    }

    public String getName(){
        return petName;
    }

    public int getAge(){
        return petAge;
    }

}

然后我有一个包含宠物集合的 ArrayList,如下面的代码所示:

import java.util.ArrayList;

pet Dog = new pet("Orio", 2);
pet Cat = new pet("Kathy", 4);
pet Lion = new pet("Usumba", 6);

ArrayList<pet> pets = new ArrayList<>();
pets.add(Dog);
pets.add(Cat);
pets.add(Lion;

我想知道如何检索 ArrayList 中的索引或具有所需名称的对象。所以如果我想知道 Usumba 的年龄,我该怎么做?

注意:这不是我的实际代码,只是为了更好地解释我的问题。

编辑 1

到目前为止,我有以下内容,但我想知道是否有更好或更有效的方法

public int getPetAge(String petName){
    int petAge= 0;

    for (pet currentPet : pets) {
        if (currentPet.getName() == petName){
            petAge = currentPet.getAge();
            break;
        }
    }

    return petAge;
}

您不能将 indexOf() 用于此目的,除非您滥用 equals() 方法的目的。

对从 0 迭代到列表长度的 int 变量使用 for 循环。

在循环内,比较第 i 个元素的名称,如果它与您搜索的词相同,则您找到了它。

像这样:

int index = -1;
for (int i = 0; i < pets.length; i++) {
    if (pets.get(i).getName().equals(searchName)) {
        index = i;
        break;
    }
}

// index now holds the found index, or -1 if not found

如果只是想找对象,不需要索引:

pet found = null;
for (pet p : pets) {
    if (p.getName().equals(searchName)) {
        found = p;
        break;
    }
}

// found is now something or null if not found

正如其他人已经指出的那样,您不能为此直接使用 indexOf() 。在某些情况下(lambda、重写 hashCode/equals 等)这是可能的,但这通常是一个坏主意,因为它会滥用另一个概念。

以下是我们如何在现代 Java 中做到这一点的几个示例: (由于索引主题已经回答得很好,这里只处理直接对象return)

package Whosebug.filterstuff;

import java.util.ArrayList;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;

public class FilterStuff {


    public static void main(final String[] args) {
        final Pet dog = new Pet("Orio", 2); // again, naming conventions: variable names start with lowercase letters
        final Pet cat = new Pet("Kathy", 4);
        final Pet lion = new Pet("Usumba", 6);

        final ArrayList<Pet> pets = new ArrayList<>();
        pets.add(dog);
        pets.add(cat);
        pets.add(lion);

        try {
            simpleOldLoop(pets);
        } catch (final Exception e) {
            e.printStackTrace(System.out);
        }
        try {
            simpleLoopWithLambda(pets);
        } catch (final Exception e) {
            e.printStackTrace(System.out);
        }
        try {
            filterStreams(pets);
        } catch (final Exception e) {
            e.printStackTrace(System.out);
        }
        try {
            filterStreamsWithLambda(pets);
        } catch (final Exception e) {
            e.printStackTrace(System.out);
        }
    }



    private static void simpleOldLoop(final ArrayList<Pet> pPets) {
        System.out.println("\nFilterStuff.simpleOldLoop()");
        System.out.println("Pet named 'Kathy': " + filterPet_simpleOldLoop(pPets, "Kathy"));
        System.out.println("Pet named 'Hans': " + filterPet_simpleOldLoop(pPets, "Hans"));
    }

    private static Pet filterPet_simpleOldLoop(final ArrayList<Pet> pPets, final String pName) {
        if (pPets == null) return null;
        for (final Pet pet : pPets) {
            if (pet == null) continue;

            if (Objects.equals(pet.getName(), pName)) return pet;
        }
        return null;
    }



    private static void simpleLoopWithLambda(final ArrayList<Pet> pPets) {
        System.out.println("\nFilterStuff.simpleLoopWithLambda()");
        System.out.println("Pet named 'Kathy': " + filterPet_simpleLoopWithLambda(pPets, (pet) -> Boolean.valueOf(Objects.equals(pet.getName(), "Kathy"))));
        System.out.println("Pet named 'Hans': " + filterPet_simpleLoopWithLambda(pPets, (pet) -> Boolean.valueOf(Objects.equals(pet.getName(), "Hans"))));
    }
    private static Pet filterPet_simpleLoopWithLambda(final ArrayList<Pet> pPets, final Function<Pet, Boolean> pLambda) {
        if (pPets == null) return null;
        for (final Pet pet : pPets) {
            if (pet == null) continue;

            final Boolean result = pLambda.apply(pet);
            if (result == Boolean.TRUE) return pet;
        }
        return null;
    }



    private static void filterStreams(final ArrayList<Pet> pPets) {
        System.out.println("\nFilterStuff.filterStreams()");
        System.out.println("Pet named 'Kathy': " + filterPet_filterStreams(pPets, "Kathy"));
        System.out.println("Pet named 'Hans': " + filterPet_filterStreams(pPets, "Hans"));
    }

    private static Pet filterPet_filterStreams(final ArrayList<Pet> pPets, final String pName) {
        return pPets.stream().filter(p -> Objects.equals(p.getName(), pName)).findAny().get();
    }



    private static void filterStreamsWithLambda(final ArrayList<Pet> pPets) {
        System.out.println("\nFilterStuff.filterStreamsWithLambda()");

        System.out.println("Pet named 'Kathy': " + filterPet_filterStreams(pPets, p -> Objects.equals(p.getName(), "Kathy")));

        final Predicate<Pet> pdctHans = p -> Objects.equals(p.getName(), "Hans"); // we can also have 'lambda expressions' stored in variables
        System.out.println("Pet named 'Hans': " + filterPet_filterStreams(pPets, pdctHans));
    }

    private static Pet filterPet_filterStreams(final ArrayList<Pet> pPets, final Predicate<Pet> pLambdaPredicate) {
        return pPets.stream().filter(pLambdaPredicate).findAny().get();
    }



}

连同你的宠物 class,由 toString() 扩展:

package Whosebug.filterstuff;

public class Pet { // please stick to naming conventions: classes start with uppercase letters!
    private final String    petName;
    private final int       petAge;

    public Pet(final String name, final int age) {
        petName = name;
        petAge = age;
    }

    public String getName() {
        return petName;
    }

    public int getAge() {
        return petAge;
    }

    @Override public String toString() {
        return "Pet [Name=" + petName + ", Age=" + petAge + "]";
    }

}