我正在尝试创建一个将类映射到字符串的ImmutableMap(注意:这当然只是一个例子!).但是,像
ImmutableMap,String> map = ImmutableMap.of(
Integer.class,"Integer",Date.class,"Date"
);
给我以下错误
Type mismatch: cannot convert from ImmutableMap&Serializable>,String> to ImmutableMap,String>
奇怪的是,如果我将一个转换添加到Class任何(!)的键,即
ImmutableMap,String> map = ImmutableMap.of(
Integer.class,"Date",(Class>) String.class,"String",long.class,"Long"
);
将工作很好.我对这个行为感到困惑:对于一个,为什么没有演员呢?所有这些都是类,它真的不会比Class更加通用,所以为什么它不起作用?其次,为什么任何一个键上的演员都可以工作?
(旁注:如果你想知道为什么我甚至想做这样的事情 – 是的,这是因为反思…)
编辑:我实际上只是想出这样会奏效,但是我想要理解上述的行为
ImmutableMap,String> map = ImmutableMap.,String>builder()
.put( Integer.class,"Integer" )
.put( Date.class,"Date" )
.build();