FindBugs warning: Inefficient use of keySet iterator instead of entrySet iterator

前端 未结 5 1565
别跟我提以往
别跟我提以往 2021-02-06 23:20

Please refer to the following method :

public Set getCellsInColumn(String columnIndex){
    Map cellsMap = getCell         


        
5条回答
  •  长发绾君心
    2021-02-06 23:30

    This is the suggestion; not really an answer for your question. When you are working with ConcurrentHashMap; below is the iterator behavior mentioned in javadoc

    The view's iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.

    So if you use EntrySet iterator; this may contain stale key/value pair; so it would be better; get the key from keySet iterator(); and check with collection for value. this will ensure you are getting the recent change from the collection.

    If you are OK with fail-safe iterator; then check this link ; it states using entrySet; little improving the performance.

提交回复
热议问题