0

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>
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>
but since Map.EntryMap.Entry<Object,Object>
why not Set<Map.Entry>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?
But in another case,for exampleArrayList 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"

jacky
  • 649
  • 1
  • 8
  • 22
  • 2
    Possible duplicate of [What is a raw type and why shouldn't we use it?](http://stackoverflow.com/q/2770321/1553851) – shmosel Dec 21 '16 at 21:02
  • 2
    See also [Is List a subclass of List? Why aren't Java's generics implicitly polymorphic?](http://stackoverflow.com/q/2745265/1553851) – shmosel Dec 21 '16 at 21:03
  • Thanks,I've find the relavent questions,but my question is this case is something different. To be more spacific,why when I use Set,I can't event get through compile and get an error but not just a warning? But in another case,for exampleArrayList li = new ArrayList(); I can get through compile and just get a warning "ArrayList is a raw type. References to generic type ArrayList should be parameterized" – jacky Dec 22 '16 at 07:10
  • That's answered in the second link. – shmosel Dec 22 '16 at 07:13

1 Answers1

1

It's because of the set<> generic type.For example,
HashSet<String> s1 = new HashSet<Integer> may goes wrong.
If the right side is a Integer generic type instance,then the reference variable's generic type in the left side cannot be String.
so String ≠ Integer like Map.Entry ≠ Map.Entry<Object,Object>

bruce
  • 78
  • 6