[英]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>>>
toMap<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.
阅读泛型教程也对您有好处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.