【问题标题】:How can I use the indexOf() function to find an object with a certain property如何使用 indexOf() 函数查找具有特定属性的对象
【发布时间】:2021-03-23 22:30:57
【问题描述】:

我有一个对象 Pet,其中一个功能是检索它的名称。

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 中的索引或具有我需要的名称的对象。所以如果我想知道乌松巴的年龄,我该怎么做呢?

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

编辑 1

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

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

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

    return petAge;
}

【问题讨论】:

    标签: java arraylist indexof


    【解决方案1】:

    您不能将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
    

    【讨论】:

    • 好的,有道理。谢谢!
    • @Ryan 请注意,您不能使用== 正确比较字符串。始终使用.equals()。另请参阅替代 impl。
    【解决方案2】:

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

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

    package stackoverflow.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();
        }
    
    
    
    }
    

    连同您的 Pet 类,由 toString() 扩展:

    package stackoverflow.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 + "]";
        }
    
    }
    

    【讨论】:

    • 非常感谢,我不明白一些功能,所以我现在正在查看文档。它们看起来非常先进但非常有用。欣赏!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 2018-05-07
    • 2017-07-07
    相关资源
    最近更新 更多