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());
}
| Modifier and Type | Field and 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.
|
| Constructor and Description |
|---|
MapIterationInForEachLoopCheck()
Creates default importList and mapImportClassesNamesList.
|
| Modifier and Type | Method and 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) |
clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeveritypublic static final String MSG_KEY_KEYSET
public static final String MSG_KEY_ENTRYSET
public static final String MSG_KEY_VALUES
public MapIterationInForEachLoopCheck()
public final void setSupportedMapImplQualifiedNames(String... setSupportedMapImplQualifiedNames)
setSupportedMapImplQualifiedNames - User's set of map implementations.public void setProposeValuesUsage(boolean proposeValuesUsage)
proposeValuesUsage - User's value of mProcessingValue.public void setProposeKeySetUsage(boolean proposeKeySetUsage)
proposeKeySetUsage - User's value of mIsCheckKeySetProcessingEnabled.public void setProposeEntrySetUsage(boolean proposeEntrySetUsage)
proposeEntrySetUsage - User's value of mIsCheckEntrySetProcessingEnabled.public int[] getDefaultTokens()
getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getAcceptableTokens()
getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getRequiredTokens()
getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void beginTree(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
beginTree in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckCopyright © 2021. All rights reserved.