简体   繁体   English

类型不匹配:无法从地图转换 <Object,Map<Object,List<ActorContents> &gt;&gt;到地图 <Actor ,Map<String,List<ActorContents> &gt;&gt;

[英]Type mismatch: cannot convert from Map<Object,Map<Object,List<ActorContents>>> to Map<Actor ,Map<String,List<ActorContents>>>

I am trying to create nested map from a list. 我正在尝试从列表创建嵌套地图。 With the below snippet I get compile time error 使用下面的代码片段,我得到编译时错误

Type mismatch: cannot convert from Map<Object,Map<Object,List<ActorContents>>> to Map<Actor,Map<String,List<ActorContents>>> 类型不匹配:无法从Map<Object,Map<Object,List<ActorContents>>>Map<Actor,Map<String,List<ActorContents>>>

 Map<Actor, List<String>> actorTypeOfContents = typeofContentforActor(genres, genreId);

            Map<Actor, Map<String, List<ActorContents>>> imageMap1=                 
                actorContents.stream()
                        .collect(Collectors.groupingBy(e -> e.getActor(), Collectors.groupingBy( p -> Utility.find(actorTypeOfContents.get(p.getActor()), i -> StringUtils.contains(p.getName(), "_" + i + "_"))
                                ))); 

Utility method used is as below 使用的实用方法如下

public static <T> T find(List<T> items, Predicate<T> matchFunction) {
        for (T possibleMatch : items) {
            if (matchFunction.test(possibleMatch)) {
                return possibleMatch;
            }
        }
        return null;
    }

When I change the code as below There is no error and code executes. 当我如下更改代码时,没有错误并且代码执行。

List<String> actorNames =actorTypeOfContents.get(Actor.Genre1);

Map<Actor, Map<String, List<ActorContents>>> imageMap1=                 
                actorContents.stream()
                        .collect(Collectors.groupingBy(e -> e.getActor(), Collectors.groupingBy( p -> Utility.find(actorNames, i -> StringUtils.contains(p.getName(), "_" + i + "_"))
                                ))); 

Could you help to figure out what is wrong with the snippet 您能帮忙找出代码段出什么问题吗

Map<Actor, Map<String, List<ActorContents>>> imageMap1=                 
                actorContents.stream()
                        .collect(Collectors.groupingBy(e -> e.getActor(), Collectors.groupingBy( p -> Utility.find(actorTypeOfContents.get(p.getActor()), i -> StringUtils.contains(p.getName(), "_" + i + "_"))
                                ))); 

Your assistance is highly appreciated 非常感谢您的协助

Lets consider the inner map Map<Object,List<ActorContents>> only, since the outer has the same issue. 我们只考虑内部映射Map<Object,List<ActorContents>> ,因为外部映射具有相同的问题。 Consider this: 考虑一下:

Map<Object,List<ActorContents>> map = new HashMap<>();
map.put(1, Arrays.asList(new ActorContents()));
map.put("one", Arrays.asList(new ActorContents()));

Now, you've a map with 2 keys that are different data types. 现在,您有了一个包含2个不同数据类型键的地图。 You're asking the compiler to convert it into a map with a specific type for the key ( Actor ). 您要让编译器将其转换为具有特定键类型( Actor )的映射。 The compiler doesn't know how to convert an integer or a string to an Actor . 编译器不知道如何将整数或字符串转换为Actor

I deliberately didn't reference your code because after reading my explanation, you should be able to figure out the problem yourself. 我故意没有引用您的代码,因为在阅读我的解释后,您应该可以自己解决问题。 It'll also do you good to read the generics tutorial. 阅读泛型教程也对您有好处。

转换列表<object>映射<div id="text_translate"><p>我有一个包含四个字段的Employee类:id、name、department 和 email。 我创建了五个对象,设置了值并将它们全部添加到列表中。</p><p> 现在,我想将该列表转换为地图,但我做不到。 供您参考,这是我尝试过的代码</p><pre>Employee emp1 = new Employee("1", "Mayank", "HR", "mayank@gmail.com"); Employee emp2 = new Employee("2", "Mahesh", "Trainer", "Mahesh@gmail.com"); Employee emp3 = new Employee("3", "Vipul", "SEO", "Vipul@gmail.com"); Employee emp4 = new Employee("4", "Ajay", "Devlopwr", "Ajay@gmail.com"); Employee emp5 = new Employee("5", "Rakesh", "Marketing", "Rakesh@gmail.com"); //add class object to list List &lt; Employee &gt; listWmp = new ArrayList &lt; &gt; (); listWmp.add(0, emp1); listWmp.add(1, emp2); listWmp.add(2, emp3); listWmp.add(3, emp4); listWmp.add(4, emp5); System.out.println("list elements are: " + listWmp); //convert the list into map Map &lt; String, Employee &gt; listMap = new HashMap &lt; &gt; (); for (Employee employee: listWmp) { listMap.put(employee.getEmpId(), employee.getEmpBame()); }</pre><p> 如何将列表转换为地图?</p></div></object> - convert List<object> to map

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:类型不匹配:无法从 Map 转换<object,list<object> &gt; 至 Map <string,list<> &gt; </string,list<></object,list<object> - ERROR: Type mismatch: cannot convert from Map<Object,List<Object>> to Map<String,List<>> 类型不匹配:无法从List转换 <Map<String,Object> &gt;列表 <Object> - Type mismatch: cannot convert from List<Map<String,Object>> to List<Object> Java Stream-编译时错误-类型不匹配:无法从Map转换 <Object,Object> 到地图 <Integer,List<String> &gt; - Java Stream - Compile time Error - Type mismatch: cannot convert from Map<Object,Object> to Map<Integer,List<String>> 黄瓜-类型不匹配:无法从元素类型映射转换 <Object,Object> 到地图 <String,String> - Cucumber - Type mismatch: cannot convert from element type Map<Object,Object> to Map<String,String> 转换列表<Map>列出<Map<String,Object> &gt; - Convert List<Map> to List<Map<String,Object>> 类型不匹配:无法从地图转换 <String,List<Animal> &gt;到地图 <String,List<Dog> &gt; - Type mismatch: cannot convert from Map<String,List<Animal>> to Map<String,List<Dog>> 转换列表<object>映射<div id="text_translate"><p>我有一个包含四个字段的Employee类:id、name、department 和 email。 我创建了五个对象,设置了值并将它们全部添加到列表中。</p><p> 现在,我想将该列表转换为地图,但我做不到。 供您参考,这是我尝试过的代码</p><pre>Employee emp1 = new Employee("1", "Mayank", "HR", "mayank@gmail.com"); Employee emp2 = new Employee("2", "Mahesh", "Trainer", "Mahesh@gmail.com"); Employee emp3 = new Employee("3", "Vipul", "SEO", "Vipul@gmail.com"); Employee emp4 = new Employee("4", "Ajay", "Devlopwr", "Ajay@gmail.com"); Employee emp5 = new Employee("5", "Rakesh", "Marketing", "Rakesh@gmail.com"); //add class object to list List &lt; Employee &gt; listWmp = new ArrayList &lt; &gt; (); listWmp.add(0, emp1); listWmp.add(1, emp2); listWmp.add(2, emp3); listWmp.add(3, emp4); listWmp.add(4, emp5); System.out.println("list elements are: " + listWmp); //convert the list into map Map &lt; String, Employee &gt; listMap = new HashMap &lt; &gt; (); for (Employee employee: listWmp) { listMap.put(employee.getEmpId(), employee.getEmpBame()); }</pre><p> 如何将列表转换为地图?</p></div></object> - convert List<object> to map 转换清单 <Map<String, Object> &gt;列出 <String> - Convert List<Map<String, Object>> to List<String> 转换地图 <String, Object> 到地图 <String, List<Object> &gt; - Convert Map<String, Object> to Map<String, List<Object>> 转换列表<map<string, comparable<> >> 列表<map<string, object> > </map<string,></map<string,> - Convert List<Map<String, Comparable<>>> to List<Map<String, Object>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM