问题描述
我有一个包含成千上万个矩形的HashSet,当Y小于0时,我想将其从HashSet中删除
I have a HashSet containing tens of thousands of rectangles, when the Y is less than 0 I want to remove it from the HashSet
现在我的代码如下所示
for (Rectangle p : point) {
if(p.y<0){
point.remove(p);
System.out.println("removing p");
continue;
}
这是我的HashSet的代码
Here is the code for my HashSet
public HashSet<Rectangle> point;
它从不删除Y小于0的Rectangle,而是System.out.println("removing p");运行.
it never removes the Rectangle that has a Y less than 0 but the System.out.println("removing p"); runs.
推荐答案
问题是HashSet不允许您删除哈希值已更改的项目.因此,更改项目几乎没有用.而是使用libGDX中内置的Array类,它将正常工作.尽管可能有人可以以更快的速度发布答案,但不确定其性能.
The problem is HashSet does not allow you to remove items that had their hash changed. So it is pretty much useless for changing items. Instead use the Array class built in libGDX and it will work fine. Not sure of the performance though maybe someone can post an answer with faster performance.
这篇关于HashSet根据条件删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!