简体   繁体   English

将map.values()返回的Collection转换为List

[英]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.

相关问题 为什么Map.values()返回抽象实现? - Why abstract implementation is returned by Map.values()? 如何获取Map.values作为集合而不是集合? - How to get Map.values as a Set rather than a Collection? 尝试使用 Map.values().parallelStream().forEach(list -&gt; list.sort(comparator)) 但出现错误:“比较方法违反其一般合同!” - Trying to use Map.values().parallelStream().forEach(list -> list.sort(comparator)) but get error: "Comparison method violates its general contract!" 如何将以List为键的映射转换为合并列表值的集合? - How to convert map with List as key to collection of merged list values? Map.keySet和Map.values上的迭代顺序相同? - Same iteration order on Map.keySet and Map.values? 为什么map.keyset()返回设置视图,而map.values()返回Java中的集合? - why map.keyset() returns set view but map.values() returns collections in Java? 为什么map.values()。stream()比Array.stream(array)慢得多 - Why is map.values().stream() much slower than Array.stream(array) 当通过 For 循环返回列表时,无法将列表转换为 Java 中的 Map - Unable to convert List to Map in Java when the List is returned via For Loop 将值列表转换为地图<key, List<Object> &gt; - Convert a list of values to a map<key, List<Object>> 如何将List的Map的值转换为单个List - How to convert the values of a Map of List to a single List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM