我有以下代码
List<Integer> startnodes = ImmutableList.of(397251,519504,539122,539123,539124,539125);
List<Integer> endnodes = ImmutableList.of(539126,539127,539142,539143,539144,539145);
List<String> rp = ImmutableList.of("Knows","Knows","Knows","Knows2","Knows2","Knows2");
Map<String,Value> parameters =
ImmutableMap.of("rels",ImmutableList.of(startnodes,endnodes,rp));
编译器在最后一行抛出以下错误。
Type mismatch: cannot convert from ImmutableMap<String,ImmutableList<List<?
extends Object&Comparable<?>&Serializable>>> to Map<String,Value>
我的主要困惑是,这里键的值是一个异构列表,那么键的类型应该是什么才能满足编译器的要求?我对 Java 比较陌生,欢迎任何建议。谢谢!
最佳答案
ImmutableList.of(startnodes,endnodes,rp)
需要推断一个同时满足 List<Integer>
的类型和List<String>
因为Java中的泛型是invariant唯一满足它的类型是 List<?>
。因此您可以将其分配给:
List<List<?>> list = ImmutableList.of(startnodes,endnodes,rp);
还有你的Map
需要定义为:
Map<String,List<List<?>>> parameters = ImmutableMap.of("rels",ImmutableList.of(startnodes,endnodes,rp));
关于java - 在Java中创建异构列表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47132328/