Class MapIterationInForEachLoopCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    public class MapIterationInForEachLoopCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck

    This check can help you to write the whole for-each map iteration more correctly.

    1. 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<String, String> 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);
     }
     

    2. 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<String, String> entry : map.entrySet())
     {
         System.out.println(entry.getKey());
     }
     

    Good:

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

    3. 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<String, String>S entry : map.entrySet())
     {
         System.out.println(entry.getValue() + "   " + entry.getKey());
     }
     
    Since:
    1.11.0
    Author:
    Max Vetrenko
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String MSG_KEY_ENTRYSET
      The key is pointing to the warning message text in "messages.properties" file.
      static String MSG_KEY_KEYSET
      The key is pointing to the warning message text in "messages.properties" file.
      static String MSG_KEY_VALUES
      The key is pointing to the warning message text in "messages.properties" file.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void beginTree​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)  
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void setProposeEntrySetUsage​(boolean proposeEntrySetUsage)
      Set aProcessingEntrySet.
      void setProposeKeySetUsage​(boolean proposeKeySetUsage)
      Set aProcessingKeySet.
      void setProposeValuesUsage​(boolean proposeValuesUsage)
      Set aProcessingValue.
      void setSupportedMapImplQualifiedNames​(String... setSupportedMapImplQualifiedNames)
      Set user's map implementations.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

        finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Field Detail

      • MSG_KEY_KEYSET

        public static final String MSG_KEY_KEYSET
        The key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
      • MSG_KEY_ENTRYSET

        public static final String MSG_KEY_ENTRYSET
        The key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
      • MSG_KEY_VALUES

        public static final String MSG_KEY_VALUES
        The key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
    • Constructor Detail

      • MapIterationInForEachLoopCheck

        public MapIterationInForEachLoopCheck()
        Creates default importList and mapImportClassesNamesList.
    • Method Detail

      • setSupportedMapImplQualifiedNames

        public final void setSupportedMapImplQualifiedNames​(String... setSupportedMapImplQualifiedNames)
        Set user's map implementations. It must state the full paths of imported classes. Import paths must be separated by commas. For example: java.util.Map, java.util.HashMap.
        Parameters:
        setSupportedMapImplQualifiedNames - User's set of map implementations.
      • setProposeValuesUsage

        public void setProposeValuesUsage​(boolean proposeValuesUsage)
        Set aProcessingValue. If value is true, Check will process cases, where values() method will be suitable.
        Parameters:
        proposeValuesUsage - User's value of mProcessingValue.
      • setProposeKeySetUsage

        public void setProposeKeySetUsage​(boolean proposeKeySetUsage)
        Set aProcessingKeySet. If value is true, Check will process cases, where keySet() method will be suitable.
        Parameters:
        proposeKeySetUsage - User's value of mIsCheckKeySetProcessingEnabled.
      • setProposeEntrySetUsage

        public void setProposeEntrySetUsage​(boolean proposeEntrySetUsage)
        Set aProcessingEntrySet. If value is true, Check will process cases, where entrySet() method will be suitable.
        Parameters:
        proposeEntrySetUsage - User's value of mIsCheckEntrySetProcessingEnabled.
      • getDefaultTokens

        public int[] getDefaultTokens()
        Specified by:
        getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Specified by:
        getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getRequiredTokens

        public int[] getRequiredTokens()
        Specified by:
        getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • beginTree

        public void beginTree​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
        Overrides:
        beginTree in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck