简体   繁体   中英

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

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

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.

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.

在Java 8,有一个getOrDefault地图方法,它应该像Python的dict s'的get方法: https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#getOrDefault- java.lang.Object -V-

    HashMap<Character, String> d = new HashMap<Character, String>();  
    System.out.println(d.getOrDefault('c', "pizza")); 

map.get("x") == null ? true : map.get("x")

这听起来不错但是如果你需要它,你可以实现自己的实现来在get()它自己处理它

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.

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 = map.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 = get(key);
        if (ret == null) {
            return defaultValue;
        }
        return ret;
    }
}

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));

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM