我正在努力找到相关的答案来解决我的问题。我有一个数组列表:
List<Teacher> teacherList = new ArrayList<Teacher>(11);
教师班级具有名称属性。我想浏览教师列表,找出是否有与用户输入名称匹配的名称。
例如,
Scanner teacherNameScanner = new Scanner (System.in);
System.out.println("What is the teachers name");
teacherName = teacherNameScanner.nextLine();
/* Here I want to scan through the teacherList and see whether the name entered
matches the name of any of the teachers. If it does not match I want to tell the
user to enter a correct name */
答案 0 :(得分:1)
这实际上取决于您的用例,名称是否区分大小写?另外,您可能需要考虑修剪空间。然后创建一个Set nameSet,您可以在其中获取O(1)操作的现有名称。同样,对此有很多解决方案,除非您描述确切的用例,例如您是否想用姓名或ID(通常是ID)来确定教师,否则很难找到正确的解决方案。但是使用您描述的HashSet应该做的事情。
Set<String> nameSet = new HashSet<String>();
if (nameSet.contains(teacherName))
do what you want
else
other case
答案 1 :(得分:0)
for each
正是医生所订购的(除非您有一些特别的命令,您可以使用它进行更有效的搜索)。
private Teacher findTheTeacher(ArrayList<Teacher> teacherList, String teacherName) {
for (Teacher teacher : teacherList) {
if (teacher.name.equals(teacherName)) {
return teacher;
}
}
return null; // or something like this
}
答案 2 :(得分:0)
要扫描teacherList
并查找是否与输入的老师姓名(teacherName
)相匹配,可以使用以下方法之一。请注意,每种方法都将teacherList
和enteredName
作为参数,并返回boolean
是或否-取决于是否找到匹配项。
共有三种方法,每种方法的执行方式都不相同。请注意,第三个结构C-使用与前两个结构A和B不同的方法。
方法A:
使用for循环迭代teacherList
并查找是否有名称匹配的教师:
for (Teacher teacher : teacherList) {
if (teacher.getName().equals(enteredName)) {
// a match is found
return true;
}
}
return false;
方法B:
它具有与方法A相同的功能,但使用不同的代码构造-流和lambda:
boolean enteredTeacherExists = teacherList.stream()
.anyMatch(teacher -> teacher.getName().equals(enteredName));
语法teacher -> teacher.getName().equals(enteredName)
是Predicate<Teacher>
类型的lambda表达式。
方法C:
此方法使用另一种方法。无需测试老师的姓名,而是使用输入的姓名构造老师对象,然后使用该对象查找匹配项。
使用输入的名称创建一名教师,并测试列表中是否存在该教师。
Teacher enteredTeacher = new Teacher(enteredName);
boolean enteredTeacherExists = teacherList.contains(enteredTeacher);
如果列表中存在同名老师,则返回的值为true,否则为false。
请注意,Teacher
类(请参见下面的定义)具有java.lang.Object
类的重写equals()
方法。此方法指定两个教师对象相等的条件-如果它们的名称相等,则认为教师对象相等。这允许使用等于List
的{{1}}方法。
代码:
Teacher.java:
contains
TeacherTester.java
public class Teacher {
private String name;
public Teacher(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
/* This returns a string representation of the teacher - the name. */
public String toString() {
return name;
}
@Override
public boolean equals(Object other) {
if ((other != null) && (other instanceof Teacher)) {
Teacher otherTeacher = (Teacher) other;
if (otherTeacher.getName().equals(this.name)) {
return true;
}
}
return false;
}
}
输出:
public class TeacherTester {
public static void main(String [] args) {
// Create some teachers
Teacher teacher1 = new Teacher("first");
Teacher teacher2 = new Teacher("second");
Teacher teacher3 = new Teacher("third");
List<Teacher> teacherList = Arrays.asList(teacher1, teacher2, teacher3);
System.out.println("Teachers: " + teacherList);
// Look for teacher - using method A
String enteredName = "ninth";
System.out.println("A. Found " + enteredName + " : " + findTeacherA(teacherList, enteredName));
enteredName = "first";
System.out.println("A. Found " + enteredName + " : " + findTeacherA(teacherList, enteredName));
// Look for teacher - using method B
enteredName = "ninth";
System.out.println("B. Found " + enteredName + " : " + findTeacherB(teacherList, enteredName));
enteredName = "second";
System.out.println("B. Found " + enteredName + " : " + findTeacherB(teacherList, enteredName));
// Look for teacher - using method C
enteredName = "third";
System.out.println("C. Found " + enteredName + " : " + findTeacherC(teacherList, enteredName));
enteredName = "ninth";
System.out.println("C. Found " + enteredName + " : " + findTeacherC(teacherList, enteredName));
}
private static boolean findTeacherA(List<Teacher> teacherList, String enteredName) {
for (Teacher teacher : teacherList) {
if (teacher.getName().equals(enteredName)) {
// a match is found
return true;
}
}
return false;
}
private static boolean findTeacherB(List<Teacher> teacherList, String enteredName) {
return teacherList.stream()
.anyMatch(teacher -> teacher.getName().equals(enteredName));
}
private static boolean findTeacherC(List<Teacher> teacherList, String enteredName) {
Teacher enteredTeacher = new Teacher(enteredName);
return teacherList.contains(enteredTeacher);
}
}
答案 3 :(得分:0)
在 Java8 中,您可以使用流:
public static Teacher findByName(Collection<Teacher> teachers, String name) {
return teachers.stream().filter(teacher -> teacher.getName().equals(name)).findFirst().orElse(null);
}
此外,如果您有许多不同的对象(不仅是Teacher
),或者想通过不同的属性(不仅是name
查找它,还可以构建一个实用程序类来封装其中的逻辑:
public final class FindUtils {
public static <T> T findByProperty(Collection<T> col, Predicate<T> filter) {
return col.stream().filter(filter).findFirst().orElse(null);
}
}
public final class TeacherUtils {
public static Teacher findByName(Collection<Teacher> teachers, String name) {
return FindUtils.findByProperty(teachers, teacher -> teacher.getName().equals(name));
}
public static Teacher findByAge(Collection<Teacher> teachers, int age) {
return FindUtils.findByProperty(teachers, teacher -> teacher.getAge() == age);
}
}
答案 4 :(得分:0)
如果您使用的是Java 8或更高版本,则可以使用:
teacherList.stream().anyMatch(teacher -> teacher.getName().equals(name));
其中name是您要搜索的名称。