java 使用来自 Apache Commons Collections 的 MultiValueMap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26215223/
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-02 09:30:37  来源:igfitidea点击:

Using a MultiValueMap from Apache Commons Collections

javaapache-commons-collection

提问by Tiny

Given below an example of org.apache.commons.collections.map.MultiValueMap(from commons-collections-3.2.1)

下面给出一个示例org.apache.commons.collections.map.MultiValueMap(来自 commons-collections-3.2.1)

Map<String, Object> multiValueMap = MultiValueMap.decorate(new HashMap<String, Object>());
multiValueMap.put("orderId", 1L);

for(Map.Entry<String, Object> entry : multiValueMap.entrySet()) {

    List<Object> value = (List<Object>) entry.getValue();
    System.out.println(entry.getKey()+" : "+value.get(0));
}

This works fine as it appears. It displays a key and the value associated with the key.

这看起来很好。它显示一个键和与该键关联的值。



If the declaration is changed as follows,

如果声明更改如下,

Map<String, Object> multiValueMap = MultiValueMap.decorate(new HashMap<String, Object>(){{
        put("orderId", 1L);
    }});

then it throws an exception -

然后它抛出一个异常 -

java.lang.ClassCastException: java.lang.Long cannot be cast to java.util.List

at the first line in the only foreachloop given.

foreach给出的唯一循环中的第一行。

In this case, the given MultiValueMapis not really a MultiValueMap. It is rather a usual HashMap.

在这种情况下,给定MultiValueMap的不是真正的 a MultiValueMap。倒是很平常HashMap

How does changing the declaration in this way make a difference?

以这种方式更改声明有何不同?

采纳答案by mikea

MultiValueMap is a fairly simple map decorator. It overrides the put methods and injects collections rather than the actual value. In the case of gets, it gets the collection from the underlying map.

MultiValueMap 是一个相当简单的地图装饰器。它覆盖 put 方法并注入集合而不是实际值。在获取的情况下,它从底层映射中获取集合。

In your second example you are decorating a populated map so when the MultiValueMap attempts to get the collection for your key, it gets a long instead:

在您的第二个示例中,您正在装饰一个填充的地图,因此当 MultiValueMap 尝试获取您的键的集合时,它会变得很长:

 public Collection  getCollection(Object key) {
    return (Collection) getMap().get(key);
}

回答by Michal

Map<String, Object> multiValueMap = MultiValueMap.decorate(new HashMap<String, Object>());
multiValueMap.put("orderId", 1L);

In the version above the put(...) is called on the variable multiValueMap.

在上面的版本中,在变量 multiValueMap 上调用了 put(...)。

Map<String, Object> multiValueMap = MultiValueMap.decorate(new HashMap<String, Object>(){{
    put("orderId", 1L);
}});

In this version the put(...) is called in within the instance initializer of the class before the doubled {{, i.e. on the HashMap.

在这个版本中,put(...) 在类的实例初始值设定项中在加倍的 {{ 之前被调用,即在 HashMap 上。

Therefore, the two pieces of code are not equivalent.

因此,这两段代码并不等价。

回答by Kumar Abhinav

Map<String, Object> multiValueMap1 = new HashMap<String, Object>() {
        {
            put("orderId",
                    new ArrayList<Object>(Arrays
                            .asList(new Object[] { 1L })));
        }
    };