Java tutorial
import java.util.HashMap; import java.util.Map; class Helper { public static <K, V> V get(Map<K, V> m, K key, V def) { V v = m.get(key); return (v != null) ? v : def; } } public class Main { public static void main(String[] args) { Map<String, Integer> m = new HashMap<String, Integer>(); m.put("A", 42); m.put("B", 3); m.put("C", 1); System.out.println(Helper.get(m, "forty-two", -1)); System.out.println(Helper.get(m, "A", -1)); System.out.println(Helper.get(m, "B", -1)); System.out.println(Helper.get(m, "something_else", -1)); } }