Apache Commons Collections库的4.0版本添加了泛型支持。我无法转换我的代码以利用它:使用自定义值集合类型创建Commons Collections MultiValueMap
我想要一个MultiValueMap
它将一个字符串作为关键字,并将字符串集合作为值。但是:
- 的关键应保留插入排序
- 值应 唯一针对每一个琴键并保留插入排序(所以我用装饰
LinkedHashMap
创建 多值图)(所以我想要的 值集合类型为LinkedHashSet
)。
我能得到的最接近的是:
MultiValueMap<String, String> orderedMap = MultiValueMap.multiValueMap(
new LinkedHashMap<String, Collection<String>>(),
LinkedHashSet.class
);
但是,产生错误:
The method
multiValueMap(Map<K,? super C>, Class<C>)
in the typeMultiValueMap
is not applicable for the arguments(LinkedHashMap<String,Collection<String>>, Class<LinkedHashSet>)
所以,现在我在泛型地狱。任何建议将是最受欢迎的。
此前4.0版本的,我做到了这一点与以下:
MultiValueMap orderedMap = MultiValueMap.decorate(
new LinkedHashMap<>(),
LinkedHashSet.class
);
简单!我提供LinkedHashMap
以用MultiValueMap
行为进行修饰,并指定用作值的集合类型(LinkedHashSet
)。但是这需要投射时,我打电话put()
和get()
,所以我想能够使用4.0提供的新的通用版本。
多数民众赞成在一个耻辱,SetValuedMap看起来像我想要的 –