今天,FindBugs提示了这样一个bug:
WMI: Inefficient use of keySet iterator instead of entrySet iterator (WMI_WRONG_MAP_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.
大致的意思就是,使用迭代器遍历entrySet
的方式才是高效的方式。应当避免Map.get(key)
,我这里胡解释一下,毕竟我们先获得keyset再用Key找Value,肯定不如把一个又一个词条entry都获得了,然后直接getValue要快……具体能快多少呢?Map越大越能看出差距来。 下面贴一个博文,简单说一下四种遍历方式。