4.0版本的发布Apache 共享集合 http://commons.apache.org/proper/commons-collections/库添加了泛型支持。我在转换代码以利用它时遇到问题:
我想要一个MultiValueMap http://commons.apache.org/proper/commons-collections/javadocs/api-release/org/apache/commons/collections4/map/MultiValueMap.html它以字符串为键,以字符串集合为值。但:
- 键应保留插入顺序(因此我创建了
通过装饰一个多值地图LinkedHashMap http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html)
- 这些值应该是
每个键都是唯一的并保留插入顺序(所以我想要
值集合类型为LinkedHashSet http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashSet.html).
我能得到的最接近的是:
MultiValueMap<String, String> orderedMap = MultiValueMap.multiValueMap(
new LinkedHashMap<String, Collection<String>>(),
LinkedHashSet.class
);
但这会产生错误:
方法multiValueMap(Map<K,? super C>, Class<C>)
在类型中MultiValueMap
不适用于参数(LinkedHashMap<String,Collection<String>>, Class<LinkedHashSet>)
所以现在我陷入了仿制药地狱。任何建议都将受到欢迎。
在 4.0 版本之前,我通过以下方式实现了这一点:
MultiValueMap orderedMap = MultiValueMap.decorate(
new LinkedHashMap<>(),
LinkedHashSet.class
);
简单的!我提供LinkedHashMap
装饰用MultiValueMap
行为,并指定集合的类型(LinkedHashSet
) 用作值。但这需要在我打电话时进行转换put()
and get()
所以我希望能够使用 4.0 提供的新通用版本。