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

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

Please refer to the following method :

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


        
5条回答
  •  心在旅途
    2021-02-06 23:35

    You are retrieving all the keys (accessing the whole map) and then for some keys, you access the map again to get the value.

    You can iterate over the map to get map entries (Map.Entry) (couples of keys and values) and access the map only once.

    Map.entrySet() delivers a set of Map.Entrys each one with the key and corresponding value.

    for ( Map.Entry< String, LIMSGridCell > entry : cellsMap.entrySet() ) {
        if ( entry.getKey().startsWith( columnIndex ) ) {
            cells.add( entry.getValue() );
        }
    }
    

    Note: I doubt that this will be much of an improvement since if you use map entries you will instantiate an object for each entry. I don't know if this is really faster than calling get() and retrieving the needed reference directly.

提交回复
热议问题