java 在 HashMap 中设置默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17220835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:20:49  来源:igfitidea点击:

Setting Default Values in HashMap

javaspringspring-mvc

提问by user2428795

I am trying to find a way to make a HashMap return a default value. For example if you look at the following this will printout "Test:=null" what if I want to request a default value so anytime I try to get something that is NOT set in the hashMap I will get the value?

我试图找到一种方法让 HashMap 返回默认值。例如,如果您查看以下内容,这将打印出“ Test:=null”,如果我想请求一个默认值,那么无论何时我尝试获取未在 hashMap 中设置的内容,我都会获取该值怎么办?

Map<String, String> test = new HashMap<String, String>();
test.put("today","monday");
System.out.println("Test =:" + test.get("hello") + "");

采纳答案by Spiff

Better check the return value instead of changing the way a Mapworks IMO. The Commons LangStringUtils.defaultString(String)method should do the trick:

最好检查返回值,而不是改变MapIMO的工作方式。在下议院郎StringUtils.defaultString(String)方法应该做的伎俩:

Map<String, String> test = new HashMap<>();
assertEquals("", StringUtils.defaultString(test.get("hello")));
assertEquals("DEFAULT", StringUtils.defaultString(test.get("hello"), "DEFAULT"));

StringUtils JavaDoc is here.

StringUtils JavaDoc 在这里

回答by Arno Mittelbach

Try the following:

请尝试以下操作:

Map<String,String> map = new HashMap<String,String>(){
    @Override
    public String get(Object key) {
        if(! containsKey(key))
            return "DEFAULT";
        return super.get(key);
    }
};

System.out.println(map.get("someKey"));

回答by Daniel

Rather than try to give a value to the data, why don't you just do a check when you want to pull the data?

与其尝试给数据赋值,不如在想拉取数据的时候做一个检查?

if (!set.containsKey(key)){
    return default_value;
}
else{
    return set.get(key);
}

回答by Visruth

No, you can't use it as a switch condition. You can override the get method by extending it into another class or you may try it as follows :

不,您不能将其用作切换条件。您可以通过将它扩展到另一个类来覆盖 get 方法,或者您可以按如下方式尝试:

Map<String, String> test = new HashMap<String, String>();
test.put("today", "monday");
String s = test.get("hello") == null? "default value" : test.get("hello");
System.out.println("Test =:" + s);

or

或者

    final String defaultValue = "default value";
    Map<String, String> test = new HashMap<String, String>() {

        @Override
        public String get(Object key) {
            String value = super.get(key);
            if (value == null) {
                return defaultValue;
            }
            return value;
        };
    };
    test.put("today", "monday");            
    System.out.println("Test =:" + test.get("nokey"));

And also you can achieve this by simply using Propertiesclass instead of HashMap.

而且您也可以通过简单地使用Propertiesclass 而不是HashMap.

        Properties properties = new Properties();
        properties.setProperty("key1", "value of key1");
        String property1 = properties.getProperty("key1", "default value");
        String property2 = properties.getProperty("key2", "default value");
        System.out.println(property1);
        System.out.println(property2);

which prints :

打印:

value of key1
default value

回答by NINCOMPOOP

Extend the HashMapand override the get().

扩展HashMap并覆盖get().