[英]Convert a map.values() returned Collection to List
I have tried everything I knew and whatever I found online but it just is not working. 我已经尝试了我所知道的一切,以及在网上找到的所有内容,但都无法正常工作。 I keep getting: 我不断得到:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.mycompany.myproject.dto.MyDto java.lang.ClassCastException:无法将java.util.LinkedHashMap强制转换为com.mycompany.myproject.dto.MyDto
I would appreciate if somebody could please tell me what I am doing wrong in the code below: 如果有人可以告诉我我在以下代码中做错了什么,我将不胜感激:
List<Map<Long, MyDto>> dtoList = restTemplate.getForObject(myRestUrl + "/some-path/dtoInfo/" + ids, List.class);
Map<Long, MyDto> myMap = dtoList.get(0);
System.out.println("myMap SIZE is: " + myMap.size());
System.out.println("myMap is: " + myMap);
List<MyDto> dtos = new ArrayList<MyDto>(myMap.values());
StringJoiner sj = new StringJoiner(",");
for(MyDto obj : dtos) {
sjStoreIds.add(obj.getId());
}
The REST call comes back with a List containing a HashMap object with the "Key" of type Long and the "Value" of type MyDto . REST调用返回一个List ,该List包含一个HashMap对象,该对象的Long类型为“ Key”,而MyDto类型为“ Value”。 Then I am printing the "Size" of the map and the map itself. 然后,我在打印地图的“大小”和地图本身。 Both prints the correct and expected information. 两者都打印正确和预期的信息。
Then I am extracting the list of values from the map as below: 然后,我从地图中提取值列表,如下所示:
List<MyDto> dtos = new ArrayList<MyDto>(myMap.values());
No issues there. 那里没有问题。
However, when I am trying to go through the object in the converted list (dtos) as below: 但是,当我尝试遍历转换后的列表(dtos)中的对象时,如下所示:
for(MyDto obj : dtos) {
sjStoreIds.add(obj.getId());
}
I get java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.mycompany.myproject.dto.MyDto for for(MyDto obj : dtos) { part. 我得到java.lang.ClassCastException:java.util.LinkedHashMap无法 强制转换 为com.mycompany.myproject.dto.MyDto for for(MyDto obj:dtos){部分。
Spinning my brains for more than 2 hours now. 现在我的大脑旋转了两个多小时。 :( :(
I am sure it's going to something very basic and I will be pissed and embarrassed once I know it but can anybody please tell me where I am doing wrong? 我相信这将是非常基本的事情,一旦我知道了我就会感到生气和尴尬,但是有人可以告诉我我在哪里做错了吗?
EDIT 编辑
Following is the REST implementation I have server-side: 以下是我在服务器端执行的REST实现:
@Override
public List<Map<Long, MyDto>> getInfo(String idValues) {
List<Map<Long, MyDto>> returnList = new ArrayList<>();
Map<Long, MyDto> result = new HashMap<>();
String[] ids = idValues.split(",");
for(String id : ids) {
result.put(Long.parseLong(id), testInfo.get(Long.parseLong(id)));
}
returnList.add(result);
return returnList;
}
The " testInfo " above is a map that gets built as result of SQL query. 上面的“ testInfo ”是作为SQL查询结果构建的映射。
restTemplate.getForObject
is returning you something that isn't actually a List<Map<Long, MyDto>>
. restTemplate.getForObject
返回的实际上不是List<Map<Long, MyDto>>
。 All the code you've shown is typesafe and correct, but getForObject
silently skips type checking in its signature, and that's biting you. 您显示的所有代码都是类型安全且正确的,但是getForObject
默默地跳过其签名中的类型检查,这getForObject
。 Figure out what type it really is. 找出真正的类型。 (From what you've shown us, it's probably a List<Map<Long, Map<Something, Something>>>
.) (根据您显示的内容,它可能是List<Map<Long, Map<Something, Something>>>
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.