Spring更高版本的HashMaps的LinkedCaseInsensitive地图投放问题 [英] LinkedCaseInsensitive map casting issue with HashMaps with Spring higher version
问题描述
LinkedCaseInsensitiveMap 是Spring框架的一部分,并扩展了 LinkedHashMap
The LinkedCaseInsensitiveMap is part of the Spring Framework and extends the LinkedHashMap
层次结构如下:
java.lang.Object
java.lang.Object
java.util.AbstractMap
java.util.AbstractMap
java.util.HashMap
java.util.HashMap
java.util.LinkedHashMap
java.util.LinkedHashMap
org.springframework.util.LinkedCaseInsensitiveMap
org.springframework.util.LinkedCaseInsensitiveMap
有关信息,请参阅:现在我有了以下代码:
List<HashMap<String, String>> l_lstResult = (List<HashMap<String, String>>)service.fetchRowwiseMultipleRecords(p_iQueryName, l_hmParams, userDetails);
l_lstCityTownList = new ArrayList<String>(l_lstResult.size());
for (int i = 0; i < l_lstResult.size(); i++) {
HashMap<String, String> l_hmColmnData = l_lstResult.get(i);
String l_sValue = l_hmColmnData.get(p_sColumnName);
l_lstCityTownList.add(l_sValue);
}
l_lstResult返回一个LinkedCaseInsensitiveMap,我在 HashMap l_hmColmnData = l_lstResult.get(i);
The l_lstResult returns a LinkedCaseInsensitiveMap and i get the error in the line HashMap l_hmColmnData = l_lstResult.get(i);
java.lang.ClassCastException: org.springframework.util.LinkedCaseInsensitiveMap无法转换为 java.util.HashMap
java.lang.ClassCastException: org.springframework.util.LinkedCaseInsensitiveMap cannot be cast to java.util.HashMap
问题是我在Spring版本4.3.14.RELEASE中收到此错误,而在3.2.3RELEASE中没有错误. 3.2.3.RELEASE中允许进行此转换的规范在哪里?
The thing is i get this error with Spring version 4.3.14.RELEASE and no error in 3.2.3.RELEASE. Where is the specification in 3.2.3.RELEASE that allows this casting.
任何建议,示例都会对我有很大帮助.
Any suggestions,examples would help me a lot .
非常感谢!
推荐答案
从Spring 4.3.6.RELEASE开始,LinkedCaseInsensitiveMap不再扩展LinkedHashMap和HashMap,而仅实现Map接口.
Since Spring 4.3.6.RELEASE, LinkedCaseInsensitiveMap doesn't extend anymore LinkedHashMap and HashMap, but only implements Map interface.
将service.fetchRowwiseMultipleRecords(p_iQueryName, l_hmParams, userDetails)
强制转换为List<HashMap<String, String>>
时,只是在告诉编译器信任 .但是,当要获取列表的第一个元素时,它会失败,因为它不是HashMap,而是LinkedCaseInsensitiveMap(不扩展HashMap),不是HashMap.
When you cast service.fetchRowwiseMultipleRecords(p_iQueryName, l_hmParams, userDetails)
to List<HashMap<String, String>>
you're just telling the compiler to trust you. But then, when it comes to get the first element of the list, it fails because it's not a HashMap but a LinkedCaseInsensitiveMap (not extending HashMap).
这将解决您的问题
List<LinkedCaseInsensitiveMap<String>> l_lstResult = service.fetchRowwiseMultipleRecords(p_iQueryName, l_hmParams, userDetails);
l_lstCityTownList = new ArrayList<String>(l_lstResult.size());
for (int i = 0; i < l_lstResult.size(); i++) {
LinkedCaseInsensitiveMap<String> l_hmColmnData = l_lstResult.get(i);
String l_sValue = l_hmColmnData.get(p_sColumnName);
l_lstCityTownList.add(l_sValue);
}
这篇关于Spring更高版本的HashMaps的LinkedCaseInsensitive地图投放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!