问题
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
right now my code looks like the following
for (Rectangle p : point) {
if(p.y<0){
point.remove(p);
System.out.println("removing p");
continue;
}
Here is the code for my HashSet
public HashSet<Rectangle> point;
it never removes the Rectangle that has a Y less than 0 but the System.out.println("removing p"); runs.
回答1:
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.
来源:https://stackoverflow.com/questions/29266476/hashset-remove-element-upon-condition