-4
private class EntryItr implements Iterator<Map.Entry<K, V>> {
... 
}

private class KeyItr<K> implements Iterator<K> {

    private Iterator<Map.Entry<K,V>> itr;

    KeyItr(){
        itr=new EntryItr(); // CANNOT CREATE
    }

Why cannot upcast EntryItr to the previously implemented interface?

freestar
  • 418
  • 3
  • 18

1 Answers1

0

This piece of code you posted does not even compile. The generics you used are not proper: the EntryItr class should be declared as EntryItr<K, V>. Also, KeyItr's itr field has an unrecognized generic value of type V. You should either change it to Object, or add it to KeyItr's generic signature, i.e. KeyItr

After these changes, the assignment itr = new EntryItr<>(); should be possible.

Please note the diamond operator here to denote that EntryItr will have same generics signature as KeyItr's itr.

JChrist
  • 1,734
  • 17
  • 23