Java - 在迭代对象列表时搜索特定属性

我正在努力寻找相关的答案来解决我的问题。我有一个数组列表:


List<Teacher> teacherList = new ArrayList<Teacher>(11);

教师类有一个 name 属性。我想扫描教师列表,并找出是否有与用户输入的名称匹配的名称。


例如,


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 */


青春有我
浏览 143回答 3
3回答

海绵宝宝撒

在Java8 中,您可以使用流:public static Teacher findByName(Collection<Teacher> teachers, String name) {&nbsp; &nbsp; return teachers.stream().filter(teacher -> teacher.getName().equals(name)).findFirst().orElse(null);}此外,如果您有许多不同的对象(不仅是Teacher),或者您想通过不同的属性(不仅是name)找到它,您可以构建一个实用程序类,以在其中封装此逻辑:public final class FindUtils {&nbsp; &nbsp; public static <T> T findByProperty(Collection<T> col, Predicate<T> filter) {&nbsp; &nbsp; &nbsp; &nbsp; return col.stream().filter(filter).findFirst().orElse(null);&nbsp; &nbsp; }}public final class TeacherUtils {&nbsp; &nbsp; public static Teacher findByName(Collection<Teacher> teachers, String name) {&nbsp; &nbsp; &nbsp; &nbsp; return FindUtils.findByProperty(teachers, teacher -> teacher.getName().equals(name));&nbsp; &nbsp; }&nbsp; &nbsp; public static Teacher findByAge(Collection<Teacher> teachers, int age) {&nbsp; &nbsp; &nbsp; &nbsp; return FindUtils.findByProperty(teachers, teacher -> teacher.getAge() == age);&nbsp; &nbsp; }}

慕容森

这真的取决于您的用例,名称是否区分大小写?此外,您可能需要考虑修剪空间。然后创建一个 Set nameSet,您可以在其中对现有名称进行获取,这是一个 O(1) 操作。同样,对此有许多解决方案,除非您描述确切的用例,例如您想用姓名还是 ID(通常是 ID)来识别教师,否则很难想出正确的解决方案。但是根据您所描述的 HashSet 应该可以。Set<String> nameSet = new HashSet<String>();if (nameSet.contains(teacherName))&nbsp; &nbsp;do what you wantelse&nbsp; &nbsp;other case

慕桂英4014372

要扫描teacherList并查找输入的教师姓名 ( teacherName)是否匹配,可以使用以下方法之一。请注意,每个方法都将 theteacherList和 theenteredName作为参数并返回booleantrue 或 false - 取决于是否找到匹配项。共有三种方法,每种方法的表现各不相同。请注意第三个构造 C - 这使用了与前两个不同的方法 - A 和 B。方法一:teacherList使用 for 循环迭代并查找是否有匹配名称的老师:for (Teacher teacher : teacherList) {&nbsp; &nbsp; if (teacher.getName().equals(enteredName)) {&nbsp; &nbsp; &nbsp; &nbsp; // a match is found&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }}return false;方法B:这与方法 A 具有相同的功能,但使用不同的代码构造 - 流和 lambda:boolean enteredTeacherExists =&nbsp; teacherList.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.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 {&nbsp; &nbsp; private String name;&nbsp; &nbsp; public Teacher(String name) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; }&nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; /* This returns a string representation of the teacher - the name. */&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean equals(Object other) {&nbsp; &nbsp; &nbsp; &nbsp; if ((other != null) && (other instanceof Teacher)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Teacher otherTeacher = (Teacher) other;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (otherTeacher.getName().equals(this.name)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }}教师测试器.javapublic class TeacherTester {&nbsp; &nbsp; public static void main(String [] args) {&nbsp; &nbsp; &nbsp; &nbsp; // Create some teachers&nbsp; &nbsp; &nbsp; &nbsp; Teacher teacher1 = new Teacher("first");&nbsp; &nbsp; &nbsp; &nbsp; Teacher teacher2 = new Teacher("second");&nbsp; &nbsp; &nbsp; &nbsp; Teacher teacher3 = new Teacher("third");&nbsp; &nbsp; &nbsp; &nbsp; List<Teacher> teacherList = Arrays.asList(teacher1, teacher2, teacher3);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Teachers: " + teacherList);&nbsp; &nbsp; &nbsp; &nbsp; // Look for teacher - using method A&nbsp; &nbsp; &nbsp; &nbsp; String enteredName = "ninth";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("A. Found " + enteredName + " : " + findTeacherA(teacherList, enteredName));&nbsp; &nbsp; &nbsp; &nbsp; enteredName = "first";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("A. Found " + enteredName + " : " + findTeacherA(teacherList, enteredName));&nbsp; &nbsp; &nbsp; &nbsp; // Look for teacher - using method B&nbsp; &nbsp; &nbsp; &nbsp; enteredName = "ninth";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("B. Found " + enteredName + " : " + findTeacherB(teacherList, enteredName));&nbsp; &nbsp; &nbsp; &nbsp; enteredName = "second";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("B. Found " + enteredName + " : " + findTeacherB(teacherList, enteredName));&nbsp; &nbsp; &nbsp; &nbsp; // Look for teacher - using method C&nbsp; &nbsp; &nbsp; &nbsp; enteredName = "third";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("C. Found " + enteredName + " : " + findTeacherC(teacherList, enteredName));&nbsp; &nbsp; &nbsp; &nbsp; enteredName = "ninth";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("C. Found " + enteredName + " : " + findTeacherC(teacherList, enteredName));&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; private static boolean findTeacherA(List<Teacher> teacherList, String enteredName) {&nbsp; &nbsp; &nbsp; &nbsp; for (Teacher teacher : teacherList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (teacher.getName().equals(enteredName)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // a match is found&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; private static boolean findTeacherB(List<Teacher> teacherList, String enteredName) {&nbsp; &nbsp; &nbsp; &nbsp; return teacherList.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .anyMatch(teacher -> teacher.getName().equals(enteredName));&nbsp; &nbsp; }&nbsp; &nbsp; private static boolean findTeacherC(List<Teacher> teacherList, String enteredName) {&nbsp; &nbsp; &nbsp; &nbsp; Teacher enteredTeacher = new Teacher(enteredName);&nbsp; &nbsp; &nbsp; &nbsp; return teacherList.contains(enteredTeacher);&nbsp; &nbsp; }}输出:Teachers: [first, second, third]A. Found ninth : falseA. Found first : trueB. Found ninth : falseB. Found second : trueC. Found third : trueC. Found ninth : false
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java