Skip to content

Instantly share code, notes, and snippets.

@yongchun
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yongchun/ce1f075736e8ec8417e4 to your computer and use it in GitHub Desktop.
Save yongchun/ce1f075736e8ec8417e4 to your computer and use it in GitHub Desktop.
map的简单工具类
/**
* 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