问题描述
LinkedCaseInsensitiveMap是Spring框架的一部分,并扩展了LinkedHashMap
层次结构如下所示:
java.lang.Object
java.util.AbstractMap
java.util.HashMap
java.util.LinkedHashMap
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);
java.lang.ClassCastException:org.springframework.util.LinkedCaseInsensitiveMap无法转换为java.util.HashMap
问题是我在Spring版本4.3.14.RELEASE中收到此错误,而在3.2.3.RELEASE中没有错误。 3.2.3.RELEASE中允许进行此转换的规范在哪里。
任何建议,例子都会对我有很大帮助。
非常感谢 !
1楼
从Spring 4.3.6.RELEASE开始,LinkedCaseInsensitiveMap不再扩展LinkedHashMap和HashMap,而仅实现Map接口。
。
当您将service.fetchRowwiseMultipleRecords(p_iQueryName, l_hmParams, userDetails)
为List<HashMap<String, String>>
您只是在告诉编译器信任您。
但是,当要获取列表的第一个元素时,它会失败,因为它不是 HashMap,而是LinkedCaseInsensitiveMap(不扩展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);
}