我试图在TreeSet中实现一个搜索方法.通过使用带有condtional的迭代器,我希望能够遍历集合并打印匹配条件的对象.然而,我现在这样做的方式是打印出后续对象而不是当前对象.这是我到目前为止:
public void getDetails() {
Iterator<Person> it = this.getPersonSet().iterator();
System.out.println("Enter First Name");
String first = in.next().toLowerCase();
System.out.println("Enter Second Name");
String last = in.next().toLowerCase();
while (it.hasNext()) {
if (it.next().getLast().toLowerCase().equals(last)) {
Person p = it.next();
System.out.println(p);
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒