当数据库查询出数据过后,发现有些数据不需要,所以这里要处理一下,加入循环list,根据判断条件去删除的话,就会出现异常。
所以这里用到了java8的新特性去删除。
List<UserFictitious> list = new ArrayList<>();
for (int i = 0; i < 50; i++){
UserFictitious userFictitious = new UserFictitious();
userFictitious.setUserFictitiousName(String.valueOf(i));
list.add(userFictitious);
}
list.removeIf(item -> item.getUserFictitiousName().equals("1"));
System.out.println(list);
底层的源码是根据迭代器的特性去删除,java8只是封装进了removeIf,简化了代码。
default boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
final Iterator<E> each = iterator();
while (each.hasNext()) {
if (filter.test(each.next())) {
each.remove();
removed = true;
}
}
return removed;
}
网友评论