What is the default value for HashMap in Java?

Java: HashMap: get: specify default value

  • In Python I can do the following: d = { 'a': True, 'b': False } print d.get('x', True) It will print True In Java, the HashMap.get method does not have the second argument for specifying a default value. What is a correct, terse and readable way of achieving this? The following expression seems too redundant to me: map.containsKey("x") ? map.get("x") : true

  • Answer:

    Both are wrong because they will do 2 lookups. Generally you'll want to do: Boolean ret = map.get("x"); if (ret == null) { return true } return ret; You can wrap this in a function public static <K,V> V mapGet(Map<K, V> map, K key, V defaultValue) { V ret = get(key); if (ret == null) { return defaultValue; } return ret; } You can make it non-static if you want. If you are the person instantiating the map object (you don't get it from exernal code) you can subclass hashmap. If an API returns map instance then you can't use subclassing. public class MyHashMap<K,V> extends HashMap<K,V> { public V get(K key, V default) { V ret = map.get(key); if (ret == null) { return defaultValue; } return ret; } }

utapyngo at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

I reckon that... Object o = map.get("x"); if(o == null) o = BOOLEAN.TRUE; ... is pretty much the most readable and efficient way of doing this. You're only doing one map lookup this way. Unfortunately, "Java" and "terse" don't tend to fit in the same sentence unless it's a negative.

G_H

OP Edited his question, but I'll leave the answer just in case. You should use map.containsKey("x") instead of map.get("x") != null to test if map has a mapping for key. Maps can contain null as valid value in mapping. The same goes for python. It's not enough to test if dict[key] == None to know if a key is mapped to something. Quick test: d = {'a': None} # returns True d['a'] == None # raises KeyError d['b'] # returns 'Whatever' d.get('b', 'Whatever') So java equivalent to python dict.get() is map.containsKey("x") ? map.get("x") : default_value as OP said

soulcheck

Both are valid solutions (although introducing a local variable would probably be better...performance-wise) but it's not a good idea to do this everywhere in your code. Consider writing an adapter for HashMap that provides you with a new get() method that allows you to specify a default value. That's the best way to enhance the functionality of a class.

Till Helge Helwig

map.get("x") == null ? true : map.get("x") This sounds good but if you need it extensively you can implement your own implementation to handle this in get() itlself

Jigar Joshi

I'd extend the map to expose a get(String key, Boolean defaultValue)... if not that I'd atleast put the terniary in a method, pushing the result of get.("x") onto the stack, instead of getting it twice... something like IfNull(map.get("x"), True) If you need to provide default you probably have a "specialised" use for the map anyway, hence I guess the wrapper-class would be the way to go, not just for this behaviour, but for any other (current AND FUTURE) specialised behaviours/responsibilities. That's just how I'd tackle it... I'm NO object-oriented design guru though ;-) Cheers. Keith.

corlettk

A short and reasonably generic way to do it, although not necessarily good practice, would be a helper class and a static import: Helper.java package vlad; import java.util.Map; public 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; } } Test.java import java.util.*; import static vlad.Helper.get; class Test{ public static void main(String[]args){ Map<String, Integer> m = new HashMap<String,Integer>(); m.put("forty-two", 42); m.put("three", 3); m.put("one", 1); System.out.println(get(m,"forty-two",-1)); System.out.println(get(m,"three",-1)); System.out.println(get(m,"one",-1)); System.out.println(get(m,"something_else",-1)); } }

Vlad

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.