【问题标题】:Java, how do I search a specific variable of Objects saved in a array listJava,如何搜索保存在数组列表中的对象的特定变量
【发布时间】:2011-10-28 15:37:39
【问题描述】:

我有一个 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&lt;Student&gt;,所以我无法搜索特定的字符串(studentID)。我该怎么做呢?如何只搜索列表中每个对象的字符串studentID? 编辑:(有类似activeStudentsList.studentID.contains(studentID) 的东西吗?)

【问题讨论】:

    标签: java search arraylist


    【解决方案1】:

    您的Student 需要正确实现equalshashCode,您可以只使用List.contains

    【讨论】:

      【解决方案2】:

      遍历列表,测试当前学生的 ID 是否与您要查找的 ID 相同。如果是,则返回 true,否则继续。在迭代结束时,返回 false。

      或者,如果您想要更简单、更快捷的操作,请使用 Map&lt;String, Student&gt; 代替列表,存储按 ID 索引的学生。如果您需要像 List 一样保留插入顺序,可以使用 HashMap 或 LinkedHashMap。

      【讨论】:

      • 我改用了 HashMap,效率更高。谢谢
      【解决方案3】:

      基于 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();
          }
      }
      

      【讨论】:

        【解决方案4】:

        假设 studentID 字符串是公开的,或者你有一个公开的 Get 方法,你可以使用 Java For-each 循环检查列表中的每个元素:

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

        【讨论】:

          【解决方案5】:

          享受您的时间并花时间使用 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;
                  }
          

          【讨论】:

            【解决方案6】:

            好的,这是我的版本。避免循环和终止条件等等,只需使用 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;
                    }
                }
            
            }
            

            这种搜索可能看起来有点头重脚轻,但我发现在更复杂的场景中使用过滤器和转换非常有用。

            【讨论】:

            • 该死。查理打败了我。
            猜你喜欢
            • 2014-10-24
            • 1970-01-01
            • 1970-01-01
            • 2019-03-10
            • 1970-01-01
            • 1970-01-01
            • 2011-05-06
            • 2012-10-17
            • 2013-02-23
            相关资源
            最近更新 更多