Last active
August 29, 2015 14:06
-
-
Save yongchun/ce1f075736e8ec8417e4 to your computer and use it in GitHub Desktop.
map的简单工具类
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A bunch of utility methods for working with maps | |
* | |
* | |
*/ | |
public final class MapHelper { | |
private MapHelper() { | |
} | |
/** | |
* Extracts the value from the map and coerces to a String | |
*/ | |
public static String getString(Map map, String key) { | |
Object answer = map.get(key); | |
return (answer != null) ? answer.toString() : null; | |
} | |
/** | |
* Extracts the value from the map and coerces to an int value or returns a | |
* default value if one could not be found or coerced | |
*/ | |
public static int getInt(Map map, String key, int defaultValue) { | |
Object value = map.get(key); | |
if (value instanceof Number) { | |
return ((Number)value).intValue(); | |
} else if (value instanceof String) { | |
return Integer.parseInt((String)value); | |
} | |
return defaultValue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment