Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

New check: PreferMapEntryToIterateWholeMap #77

Closed
romani opened this Issue · 1 comment

3 participants

@romani
Owner

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:

for (Map.Entry; entry : map.entrySet()){
   System.out.println(entry.getValue());
}
for (String key : map.keySet(){
   System.out.println(map.get(key));
}

Good:

for (String value : map.values()){
   System.out.println(value);
}

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:

for (Map.Entry entry : map.entrySet()){
   System.out.println(entry.getKey());
}

Good:

for (String key : map.keySet()){
   System.out.println(key);
}

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:

for (String key : map.keySet()){
   System.out.println(key + "  " + map.get(key));
}

Good:

for (Map.Entry entry : map.entrySet()){
   System.out.println(entry.getValue() + "   " + entry.getKey());
}
@daniilyar
Owner

I think, this is a useful check (idea was taken from the same-named FindBugs Check).

The main check idea is:

  1. To force developer to iterate over maps using Map.Entry instead of [ keyset() + getValue(key) ] for every map entry in case if developer needs both keys and values.
  2. To force developer to use keySet() method in case if developer does not use the appropriate values ( == if map.getValue(key) method is never executed inside a loop).

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:
http://www.sergiy.ca/how-to-iterate-over-a-map-in-java/

http://stackoverflow.com/questions/12639259/findbugs-warning-inefficient-use-of-keyset-iterator-instead-of-entryset-iterato

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 maxvetrenko was assigned
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. PreferMapEntryToIterateWholeMapCheck was introduced 731772a
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. PreferMapEntryToIterateWholeMapCheck was introduced 1b6d074
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. PreferMapEntryToIterateWholeMapCheck was introduced 440a96b
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. PreferMapEntryToIterateWholeMapCheck was introduced 69e52e1
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. PreferMapEntryToIterateWholeMapCheck was introduced 52775c5
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. PreferMapEntryToIterateWholeMapCheck was introduced 99d3131
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. PreferMapEntryToIterateWholeMapCheck was introduced a30e35b
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. PreferMapEntryToIterateWholeMapCheck was introduced f1f608c
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. PreferMapEntryToIterateWholeMapCheck was introduced 1fcf22b
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. PreferMapEntryToIterateWholeMapCheck was introduced 90cc3ce
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. PreferMapEntryToIterateWholeMapCheck was introduced 6fc1dcd
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. PreferMapEntryToIterateWholeMapCheck was introduced f97b5f2
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. PreferMapEntryToIterateWholeMapCheck was introduced 69295ce
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced f2d33d6
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced 9a1eb62
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced 61c4086
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced 81019fa
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced c86ce93
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced ef67ecd
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced c609d57
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced a9b4847
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. PreferMapEntryToIterateWholeMapCheck was introduced
Fixed #77. MapIterationInForEachLoop was introduced
890e1f4
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced 10e3c71
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. PreferMapEntryToIterateWholeMapCheck was introduced
Fixed #77. MapIterationInForEachLoop was introduced
98448de
@maxvetrenko maxvetrenko referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@maxvetrenko maxvetrenko referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced c30811f
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced fba1a75
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced eb6ba1f
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced df1bd9b
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced a9d9298
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced 7adc0ca
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced f5255c1
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced d19d5a8
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced 51ed043
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced 3ce6bc4
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced 58560c2
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced e885fbe
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced 5327aa8
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced 8d051e0
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced 09b2e62
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced 4e03361
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced 33d8426
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced bb15a2c
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced bd8e158
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced 31bbf12
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced 814fd81
@maxvetrenko maxvetrenko referenced this issue from a commit in maxvetrenko/sevntu.checkstyle
@maxvetrenko maxvetrenko Fixed #77. MapIterationInForEachLoop was introduced dca96ff
@daniilyar daniilyar closed this in 474a5ef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.