优化ArrayList.removeAll
很多人都说 ArrayList.removeAll 对于大尺寸数组来说确实很慢。
这篇文章提供了两种针对ArrayList.removeAll速度的优化解决方案,但需要在类本身中实现它们,并且不能在外部用作修复。
除了复制 ArrayList 源代码并使用我自己的版本之外,是否有任何方法可以应用此类修复?
编辑:我想我应该添加对此的需求,因为可能有一种方法可以在不使用 ArrayList.removeAll 的情况下完成我想要的操作。
我有两个列表,每个列表大约有 70,000 个长头
。它们几乎相同,但一个列表多了一些第二个列表没有的数字,我想找到它们。我知道找到它们的唯一方法是执行 first.removeAll(second)
来查找差异。还有别的办法吗?
A lot of people have said that ArrayList.removeAll
is really slow with large size arrays.
This article provides two optimized solutions to the ArrayList.removeAll speed, but requires implementing them in the class itself, and cannot be used externally as a fix.
Is there any way to apply this sort of fix short of copying the ArrayList source code and using my own version of it?
Edit: I suppose I should add my need for this, as there is probably a way to do what I want without ArrayList.removeAll.
I have two lists of around 70,000 longs
each. They are almost identical, but one list has a few more numbers that the second list doesn't have, and I want to find them. The only way I know to find them is to do first.removeAll(second)
to find the difference. Is there another way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用具有更好删除时间的数据结构(例如 HashSet 或 TreeSet)怎么样?因此,使用数组列表的一个重要原因是访问记录的快速访问时间为 O(1)。但如果你想设置差异那么也许你应该使用集合。只是一个想法。
What about using a data structure that has a much better removal time, such as HashSet or TreeSet? So the big reason to use an arraylist is due to the fast access time O(1) to access records. But if you are trying to set difference then maybe you should use sets. Just a thought.
您可以创建 ArrayList 的子类来优化该方法(也可能是其他方法)。
You can create a subclass of
ArrayList
to optimize that method (and possibly others).