Skip to content

Instantly share code, notes, and snippets.

@gunith
Created January 26, 2012 16:56
Show Gist options
  • Save gunith/1683766 to your computer and use it in GitHub Desktop.
Save gunith/1683766 to your computer and use it in GitHub Desktop.
Logic is, check for a value in the map... If it is there, get it. Otherwise, get a default.
/**
* Get a value from a {@link Map} or get a default value
*
* @param <K> Type of Key
* @param <V> Type of Value
* @param map The {@link Map}
* @param key The key in the {@link Map}
* @param defaultValue The default value for the key in the {@link Map}
* @return The value from {@link Map}
*/
public static <K, V> V getValueFromMap(final Map<K, V> map, final K key, final V defaultValue)
{
return map.containsKey(key) ? map.get(key) : defaultValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment