【发布时间】:2019-04-13 18:48:57
【问题描述】:
假设我有一个 Employee 对象列表,并且每个 Employee 类都有诸如 employeeName、employeeAddress、salary 等属性。现在我必须删除名称为“John”且 Salary > 40000 的 Employee 对象。
List empList = new ArrayList(); // 将百万员工添加到 empList。
根据我的理解删除具有上述条件的员工,我应该使用以下代码:
Iterator<Employee> iterator = list.iterator();
while (iterator.hasNext()) {
Employee employee = iterator.next();
if ("John".equals(employee.getName) && employee.getSalary>40000) {
iterator .remove();
}
}
所以基本上上面的代码将从列表中删除所需的 Employee 对象。 如果我的理解正确,请告诉我。
除此之外,请澄清以下内容: 1. 当我们有数百万条记录时,我们将如何解决这个问题。 2.iterator.remove()和list.remove()的区别
提前致谢。
【问题讨论】:
标签: jakarta-ee collections iterator