错误消息的含义是什么:返回类型不兼容

时间:2017-12-06 14:29:51

标签: java hashmap

我正在尝试为自定义HashMap创建entrySet()方法。我附上了我的入门课程,名为MyEntry和我的entrySet()方法。

我收到错误:

  

返回类型与Map<K,V>.entrySet()

不兼容

...并且返回类型以红色下划线。我不明白这是什么表达。

@Override   
public Set<HashMap<K,V>.MyEntry<K, V>> entrySet(){  
if (entrySetView != null) return entrySetView;
    else return entrySetView = new AbstractSet<HashMap<K,V>.MyEntry<K,V>>() {
        @Override
        public Iterator<HashMap<K,V>.MyEntry<K, V>> iterator() {
            return new HashIterator<HashMap<K,V>.MyEntry<K, V>>() {
                @Override
                public HashMap<K,V>.MyEntry<K, V> next() {
                    return nextEntry();
                }
            };
        }

        @Override
        public int size() {
            return HashMap.this.size();
        }

        @Override
        public void clear() {
            HashMap.this.clear();
        }
    };
}

public class MyEntry<K, V> implements Map.Entry<K,V>{
    private K key;
    private V value;
    private MyEntry<K,V> next;
    private int hash;

    public MyEntry() {
        counter++;
    }

    public MyEntry(K key, V value, MyEntry<K,V> next, int h) {
        this.key = key;
        this.value = value;
        this.next = next;
        this.hash = h;
        counter++;
    }

    public boolean equals(Object o) {
        if(this == o) return true;
        if (!(o instanceof MyEntry))
            return false;
        MyEntry entry = (MyEntry)o;
        Object k1 = getKey();
        Object k2 = entry.getKey();
        if (k1 == k2 || (k1 != null && k1.equals(k2))) {
            Object v1 = getValue();
            Object v2 = entry.getValue();
            if (v1 == v2 || (v1 != null && v1.equals(v2)))
                return true;
        }
        return false;
    }

    public K getKey() {
        return key;
    }
    public V getValue() {
        return value;
    }

    public void setKey(K key) {
        this.key = key;
    }

    public Object setValue(Object val) {
        V oldValue = value;
        value = (V) val;
        return oldValue;
    }

    public MyEntry<K,V> getNext(){
        return next;
    }

    public void setNext(MyEntry<K,V> n) {
        this.next = n;
    }

    public String toString() {
        return "{" + getKey() + "," + getValue() + "}";
    }
}

1 个答案:

答案 0 :(得分:4)

entrySet界面的

Map返回类型为Set<Map.Entry<K, V>>,因此您无法返回Set<HashMap<K,V>.MyEntry<K, V>>

Set<HashMap<K,V>.MyEntry<K, V>>不是Set<Map.Entry<K, V>>的子界面。

我相信这样的事情应该有用(您可能还需要更改entrySetView的类型):

@Override   
public Set<Map.Entry<K, V>> entrySet(){  
  if (entrySetView != null) 
    return entrySetView;
  else 
    return entrySetView = new AbstractSet<Map.Entry<K, V>>() {
        @Override
        public Iterator<Map.Entry<K, V>> iterator() {
            return new HashIterator<Map.Entry<K, V>>() {
                @Override
                public Map.Entry<K, V> next() {
                    return nextEntry();
                }
            };
        }

        @Override
        public int size() {
            return HashMap.this.size();
        }

        @Override
        public void clear() {
            HashMap.this.clear();
        }
    };
}