Description
WMI: Method makes inefficient use of keySet iterator instead of entrySet iterator
This method accesses the value of a Map entry, using a key that was retrieved from a keySet iterator. It is more efficient to use an iterator on the entrySet of the map, to avoid the Map.get(key) lookup.
先取得keyset去做loop,再透過key去找對應value,造成每次都需要做搜尋的動作。
Solution
要看Code的實做方式決定如何修改。如果loop內要用到value與key,可以用entrySet取代使用keySet的lookup;如果只用到value或key,我認為可以用valueset或keyset取代。
Example
Before:
for (String id: users.keySet()){ IUser user = users.get(id); logger.debug("check user {} info.", id); validateUserInfo(result, user); }
After:
for( Entry<String, IUser> entry : users.entrySet()){ logger.debug("check user {} info.", entry.getKey()); validTimeRanges(result, entry.getValue()); }
留言
張貼留言