使用自定义值集合类型创建Commons Collections MultiValueMap [英] Creating a Commons Collections MultiValueMap with a custom value collection type

查看:609
本文介绍了使用自定义值集合类型创建Commons Collections MultiValueMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apache Commons Collections 库的4.0版本增加了对泛型的支持。我无法转换我的代码以利用它:



我想要一个 MultiValueMap 字符串作为键,以及作为值的字符串集合。但是:
$ b


  1. 这些键应该保留插入顺序(所以我通过装饰一个 LinkedHashMap

  2. 对于每个键,这些值应该是
    唯一,并保留插入顺序(所以我希望
    值Collection类型为 LinkedHashSet ) 。

我能得到的最接近的是:

  MultiValueMap< String,String> orderedMap = MultiValueMap.multiValueMap(
new LinkedHashMap< String,Collection< String>>(),
LinkedHashSet.class
);

但是这会产生错误:


类型
> MultiValueMap
不适用于参数
(LinkedHashMap< String,Collection< String>> ;, Class< LinkedHashSet>) / p>

所以现在我在泛型地狱中。任何建议都会受到欢迎。



在版本4.0之前,我完成了以下操作:

<$ p $ (
new LinkedHashMap<>(),
LinkedHashSet.class
);

简单!我使用 MultiValueMap 行为提供 LinkedHashMap 来装饰,并指定集合的​​类型( LinkedHashSet )用作值。但是这需要在我调用 put() get()时进行投射,所以我希望能够使用4.0提供的新的通用版本。

解决方案

我咨询了Apache Commons Collections 邮件列表,其中它向我解释过 MultiValueMap 的接口已知缺少,但会在版本4.1中进行修改(有关JIRA问题和相关讨论,请参阅此处)。



因此,在未来我们可能会有更好的解决方案,但同时,正如Rohit Jain在他的回答中所提到的,我们只需要抑制一些警告。但是,由于类型安全的关键方面是 MultiValueMap (不是自定义集合类型),所以实现此目的的最简单方法是:

  @SuppressWarnings({rawtypes,unchecked})
MultiValueMap< String,String> orderedMap =
MapUtils.multiValueMap(new LinkedHashMap(),LinkedHashSet.class);

请注意使用 MapUtils 工厂方法,而不是我在原始问题中使用的更直接的 MultiValueMap


The 4.0 release of the Apache Commons Collections library has added generics support. I am having trouble converting my code to take advantage of it:

I would like a MultiValueMap which takes a String as a key, and a collection of Strings as the value. But:

  1. The keys should retain insertion ordering (so I create the multi-valued map by decorating a LinkedHashMap)
  2. The values should be unique for each key and retain insertion ordering (so I want the values Collection type to be a LinkedHashSet).

The closest I can get is:

MultiValueMap<String, String> orderedMap = MultiValueMap.multiValueMap(
    new LinkedHashMap<String, Collection<String>>(), 
    LinkedHashSet.class
);

But that produces the error:

The method multiValueMap(Map<K,? super C>, Class<C>) in the type MultiValueMap is not applicable for the arguments (LinkedHashMap<String,Collection<String>>, Class<LinkedHashSet>)

So now I am in generics hell. Any suggestions would be most welcome.

Prior to version 4.0, I accomplished that with the following:

MultiValueMap orderedMap = MultiValueMap.decorate(
    new LinkedHashMap<>(), 
    LinkedHashSet.class
);

Simple! I provide the LinkedHashMap to decorate with MultiValueMap behaviour, and specify the type of collection (LinkedHashSet) to use as the values. But that requires casting when I call put() and get() and so I'd like to be able to use the new generic version provided by 4.0.

解决方案

I consulted the Apache Commons Collections mailing list, where it was explained to me that the interface for MultiValueMap is known to be lacking, but will be revamped in version 4.1 (see here for the JIRA issue and associated discussion).

So in the future we may have a better solution, but in the meantime, as Rohit Jain mentioned in his answer, we're just going to have to suppress some warnings. However, since the key aspect of type safety is for the MultiValueMap (not the custom collection type), the simplest way to achieve this is:

@SuppressWarnings({ "rawtypes", "unchecked" })
MultiValueMap<String, String> orderedMap = 
    MapUtils.multiValueMap(new LinkedHashMap(), LinkedHashSet.class);

Note the use of the MapUtils factory method, rather than the more direct MultiValueMap which I had used in my original question.

这篇关于使用自定义值集合类型创建Commons Collections MultiValueMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆