3 回答
TA贡献1809条经验 获得超8个赞
在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);
}
}
TA贡献1853条经验 获得超18个赞
这真的取决于您的用例,名称是否区分大小写?此外,您可能需要考虑修剪空间。然后创建一个 Set nameSet,您可以在其中对现有名称进行获取,这是一个 O(1) 操作。同样,对此有许多解决方案,除非您描述确切的用例,例如您想用姓名还是 ID(通常是 ID)来识别教师,否则很难想出正确的解决方案。但是根据您所描述的 HashSet 应该可以。
Set<String> nameSet = new HashSet<String>();
if (nameSet.contains(teacherName))
do what you want
else
other case
TA贡献1871条经验 获得超13个赞
要扫描teacherList并查找输入的教师姓名 ( teacherName)是否匹配,可以使用以下方法之一。请注意,每个方法都将 theteacherList和 theenteredName作为参数并返回booleantrue 或 false - 取决于是否找到匹配项。
共有三种方法,每种方法的表现各不相同。请注意第三个构造 C - 这使用了与前两个不同的方法 - A 和 B。
方法一:
teacherList使用 for 循环迭代并查找是否有匹配名称的老师:
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)是一个类型为 的 lambda 表达式Predicate<Teacher>。
方法C:
此方法使用不同的方法。不是测试教师的姓名 - 使用输入的姓名构造教师对象,该对象用于查找匹配项。
根据输入的名字创建一个教师,并测试它是否存在于列表中。
Teacher enteredTeacher = new Teacher(enteredName);
boolean enteredTeacherExists = teacherList.contains(enteredTeacher);
如果列表中存在同名教师,则返回值为真,否则为假。
请注意Teacher该类(参见下面的定义)有一个java.lang.Object类的重写equals()方法。此方法指定两个教师对象相等的标准 - 如果它们的名称相等,则教师对象被视为相等。这允许使用 equalsList的contains方法。
编码:
老师.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;
}
}
教师测试器.java
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);
}
}
输出:
Teachers: [first, second, third]
A. Found ninth : false
A. Found first : true
B. Found ninth : false
B. Found second : true
C. Found third : true
C. Found ninth : false
添加回答
举报