如何在没有循环和更少时间复杂度的情况下从数组列表中删除一些项目? [英] How to remove some items from an arraylist without loop and less time complexity?

查看:18
本文介绍了如何在没有循环和更少时间复杂度的情况下从数组列表中删除一些项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 100 000 个对象的数组列表.我想删除一些具有特定 ID 的对象.我正在使用 for 循环来搜索 ArrayList 并删除它们中的每一个.因此,它非常耗时.有没有任何可能的方法来做到这一点而没有任何循环?

I have an array list of 100 000 objects. I want to remove some of the objects which have a particular ID. I'm using for loop to search through the ArrayList and remove each of them. Hence it is very much time-consuming. Is there any possible way to do this without any looping?

推荐答案

List<Type> list = ...
list.removeIf(item -> item.getId() == something);

顾名思义,removeIf() 删除所有满足谓词的元素.

As the name suggests, removeIf() removes all elements if they satisfy the predicate.

这篇关于如何在没有循环和更少时间复杂度的情况下从数组列表中删除一些项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆