HashSet.remove() and Iterator.remove() not working

前端 未结 10 1500
借酒劲吻你
借酒劲吻你 2020-12-13 00:32

I\'m having problems with Iterator.remove() called on a HashSet.

I\'ve a Set of time stamped objects. Before adding a new item to the Set, I loop through the set, i

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 00:56

    Thanks for all the help. I suspect the problem must be with equals() and hashCode() as suggested by spencerk. I did check those in my debugger and with unit tests, but I've got to be missing something.

    I ended up doing a workaround-- copying all the items except one to a new Set. For kicks, I used Apache Commons CollectionUtils.

        Set tempResults = new HashSet();
        CollectionUtils.select(allResults, 
                new Predicate()
                {
                    public boolean evaluate(Object oldData)
                    {
                        return !data.equalsData((DataResult) oldData);
                    }
                }
                , tempResults);
        allResults = tempResults;
    

    I'm going to stop here-- too much work to simplify down to a simple test case. But the help is miuch appreciated.

提交回复
热议问题