HashSet remove element upon condition

只愿长相守 提交于 2019-12-11 14:19:42

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!