[英]Map.entry and entrySet() generic type goes wrong
Here is the gone-wrone code 这是失败的代码
Set<Map.Entry> entrySet = new HashMap<Object,Object>().entrySet();
it comes up with a compile error 它带有一个编译错误
cannot convert from Set<Map.Entry<Object,Object>> to Set<Map.Entry>
But there should be just a warning from not using generic type in the case Map.Entry
from Map.Entry<Object,Object>
但是,应该只是从的情况下不使用泛型类型的警告
Map.Entry
从Map.Entry<Object,Object>
But why here come out a compile error? 但是,为什么会出现编译错误?
then I've suspected that the whole Set<Map.Entry>
may be the reason,that is maybe Set<Map.Entry>
≠ set<Map.Entry<Object,Object>
那么我怀疑整个
Set<Map.Entry>
可能是原因,可能是Set<Map.Entry>
≠ set<Map.Entry<Object,Object>
but since Map.Entry
≈ Map.Entry<Object,Object>
但是由于
Map.Entry
Map.Entry<Object,Object>
why not Set<Map.Entry>
≈ set<Map.Entry<Object,Object>
为什么不
Set<Map.Entry>
≈set set<Map.Entry<Object,Object>
I've find the relavent questions,but my question is this case is something different. 我找到了相关问题,但我的问题是这种情况有所不同。
To be more spacific,why when I use Set<Map.Entry>
,I can't event get through compile and get an error but not just a warning? 为了更加具体化,为什么当我使用
Set<Map.Entry>
,事件无法通过编译并得到错误,而不仅仅是警告?
But in another case,for example ArrayList li = new ArrayList<String>();
但是在另一种情况下,例如
ArrayList li = new ArrayList<String>();
I can get through compile and just get a warning "ArrayList is a raw type. References to generic type ArrayList should be parameterized" 我可以通过编译得到警告“ ArrayList是原始类型。对泛型类型ArrayList的引用应参数化”
It's because of the set<> generic type.For example, 这是因为有set <>泛型类型。例如,
HashSet<String> s1 = new HashSet<Integer>
may goes wrong. HashSet<String> s1 = new HashSet<Integer>
可能出错。
If the right side is a Integer generic type instance,then the reference variable's generic type in the left side cannot be String. 如果右侧是Integer通用类型实例,则左侧引用变量的通用类型不能为String。
so String ≠ Integer
like Map.Entry ≠ Map.Entry<Object,Object>
所以
String ≠ Integer
就像Map.Entry ≠ Map.Entry<Object,Object>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.