簡體   English   中英

Java,我如何搜索保存在數組列表中的對象的特定變量

[英]Java, how do I search a specific variable of Objects saved in a array list

我有一個Personnel類,其中存儲了學生,教授,家庭教師類型的所有對象。

class Personnel {
     ArrayList<Student> activeStudentsList = new ArrayList<Student>();
}

我有一個班級學生

class Student extends Person {
    public Student (String studentID, String firstName, String lastName) {
        super(firstName, lastName);
        this.studentID = studentID;
    }
}

現在,在將學生添加到數組列表之前,我想做的是檢查他是否已經在那兒。 我正在使用這個:

private boolean isStudentActive(String studentID) {
    if (activeStudentsList.contains(studentID)) {
        System.out.println("The student " + studentID + " is already on the list.");
        return true;
    } else
        return false; 
}

問題是,我的數組列表是ArrayList<Student> ,所以我無法搜索特定的String( studentID )。 我該怎么做呢? 如何僅搜索studentID中每個對象的String studentID 編輯:(是否有類似activeStudentsList.studentID.contains(studentID) ?)

您的Student需要正確地實現equalshashCode ,您可以只使用List.contains

遍歷列表,測試當前學生的ID是否等於您要查找的ID。 如果是,則返回true,否則繼續。 在迭代結束時,返回false。

或者,如果您想更輕松,更快地使用,請使用Map<String, Student>而不是列表,以存儲按其ID索引的學生。 如果您需要像List一樣保留插入順序,則可以使用HashMap或LinkedHashMap。

一個非常簡單的實現,基於Bhesh提到的使equals和hashcode使用學生ID:

class Student extends Person {

    private String studentID;

    public Student (String studentID, String firstName, String lastName) {
        super(firstName, lastName);
        this.studentID = studentID;
    }

    @Override
    public boolean equals(Object object) {
        if(object instanceof Student) {
            Student s = (Student) object;
            return this.studentID.equals(s.studentID);
        }
        return false;
    }

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

假設studentID字符串是公共的,或者您具有公共的Get方法,則可以使用Java For-each循環檢查列表中的每個元素:

for (Student s : activeStudentsList)
{
   if (s.studentID == studentID)
      return true;
}
return false;

祝您玩得開心,花點時間使用Guava庫: http : //code.google.com/p/guava-libraries/

try
        {
            Person item = Iterables.find(this.personList,
                    new Predicate<Person>() {
                        public boolean apply(Person q)
                        {
                            return itemId.equals(q.getId());
                        }
                    });
            return item;
        } catch (NoSuchElementException exception)
        {
            return null;
        }

好,這是我的版本。 避免循環和終止條件等,而只需使用Google的Guava方法進行功能編程:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.TreeMultimap;

public class TestClass {

    class Person {
        private String firtName = null;
        private String lastName = null;

        public Person(String firtName, String lastName) {
            super();
            this.firtName = firtName;
            this.lastName = lastName;
        }
    };

    class Student extends Person {
        private String studentID = null;

        public Student(String studentID, String firstName, String lastName) {
            super(firstName, lastName);
            this.studentID = studentID;
        }

        public String getStudentID() {
            return studentID;
        }
    }

    class Personnel {
        ArrayList<Student> activeStudentsList = new ArrayList<Student>();

        public boolean isStudentActive(final String studentID) {
            return Iterables.size(Iterables.filter(activeStudentsList,
                    new Predicate<Student>() {
                        @Override
                        public boolean apply(Student arg0) {
                            return arg0.getStudentID().equals(studentID);
                        }
                    })) == 1;
        }
    }

}

這種搜索似乎有點繁重,但是我發現在更復雜的情況下使用“過濾器和變換”非常有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM