开发者

does using remove on iterator makes code to work very slow?

I have a code as follows, it uses remove on a iterator as show:

Collection<X> getda开发者_如何学JAVAta(){
   Collection<X> Xs = this.getXs();
    Iterator<X> iterator = Xs.iterator();
    while (iterator.hasNext()) {
        X x = iterator.next();
        if (somecondition) {
            iterator.remove();
            }
        }
    }

 return Xs;
}

It works some times and sometime it takes very long time, and my thread hangs. getXs method returns data from DB.


Iteration and removal won't have the abnormal behavior you describe in and of itself. There are other, environmental conditions at work. Since you describe interacting with a database, chances are good that the delays and hangs are related to the way your code is interacting with the data. There are countless variations on what may be the actual problem, so any additional detail you can provide (as opposed to the very general example you posted) will be helpful in troubleshooting your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜