New check: PreferMapEntryToIterateWholeMap #77
Closed
Comments
I think, this is a useful check (idea was taken from the same-named FindBugs Check). The main check idea is:
Such type of refactoring can improve performance when it is done in frequently-used code. But if developer needs keys only for his algorithm, we should not prevent this, or show any warnings. Code examples: and: Map<String, String> map = Maps.newLinkedHashMap();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
System.out.println("\nUsing map.keySet():");
for (String key : map.keySet()) {
System.out.println(key + " --> " + map.get(key));
}
System.out.println("\nUsing Map.Entry:");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " --> " + entry.getValue());
} |
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Oct 3, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Oct 5, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Oct 7, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Oct 7, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Oct 9, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Oct 9, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Oct 9, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Oct 11, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Oct 11, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Oct 11, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Oct 11, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Oct 15, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Oct 23, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Oct 23, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Nov 3, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Nov 6, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Nov 6, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Nov 6, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Dec 8, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Dec 13, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Dec 26, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Dec 26, 2013
…introduced Fixed sevntu-checkstyle#77. MapIterationInForEachLoop was introduced
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Dec 26, 2013
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Jan 8, 2014
…introduced Fixed sevntu-checkstyle#77. MapIterationInForEachLoop was introduced
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Jan 8, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Jan 8, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Jan 8, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Jan 9, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Jan 13, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Jan 14, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Jan 16, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Jan 18, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Jan 20, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Jan 21, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Jan 25, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Jan 27, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Feb 2, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Feb 5, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Feb 5, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Feb 16, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Feb 17, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Feb 26, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Mar 1, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Mar 4, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Mar 4, 2014
maxvetrenko
added a commit
to maxvetrenko/sevntu.checkstyle
that referenced
this issue
Mar 6, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Consider user Map.Entry for whole map iteration instead of
getKeys() and then get value by key.
check could simply check while/for for such case. This check will not find all
weird cases but will help significantly with major cases, that are done as C++ habit, refactoring, .....
http://sourceforge.net/p/checkstyle/feature-requests/586/
from Check JavaDoc:
This check can help you to write the whole for-each map iteration more correctly:
If you iterate over a map using map.keySet() or map.entrySet(), but your code uses only map values, Check will propose you to use map.values() instead of map.keySet() or map.entrySet(). Replacing map.keySet() or map.entrySet() with map.values() for such cases can a bit improve an iteration performance.
Bad:
Good:
If you iterate over a map using map.entrySet(), but never call entry.getValue(), Check will propose you to use map.keySet() instead of map.entrySet(). to iterate over map keys only.
Bad:
Good:
If you iterate over a map with map.keySet() and use both keys and values, check will propose you to use map.entrySet() to improve an iteration performance by avoiding search operations inside a map. For this case, iteration can significantly grow up a performance.
Bad:
Good:
The text was updated successfully, but these errors were encountered: