【问题标题】:Java Generic HashMap implementation: Object cannot be converted VJava Generic HashMap 实现:无法转换对象 V
【发布时间】:2015-10-25 09:26:13
【问题描述】:

我正在尝试实现一个泛型 HashMap,但由于某种原因,java 编译器不允许我返回正确的泛型类型。

这是我的 HashMap 代码:

public class SimpleHashMap<K,V> {
  private int tableSize;
  private HashEntry[] table;

  public SimpleHashMap(){
    table = new HashEntry[tableSize];
    for(int i = 0; i < table.length; i++){
      table[i] = null;
    }
  }

  public V put(K key, V value){
    int keyIndex = getHashCode(key);
    if(table[keyIndex] == null){
      table[keyIndex] = new HashEntry<K, V>(key, value);
    }
    else{
      table[keyIndex] = new HashEntry<K, V>(key, value, table[keyIndex]);
    }
    return value;
  }

  public V get(K key){
    int keyIndex = getHashCode(key);
    if(table[keyIndex] == null){
      return null;
    }
    else{
      HashEntry temp = table[keyIndex];
      while(temp != null){
        if(temp.key.equals(key)){
          return temp.value;
        }
        temp = temp.next;
      }
    }
  }

  public int getHashCode(K key){
    return key.hashCode() % tableSize;
  }
}

这是我的 HashEntry 代码:

public class HashEntry<K,V>{
  public K key;
  public V value;
  public HashEntry next;

  public HashEntry(K key, V value){
    this(key, value, null);
  }

  public HashEntry(K key, V value, HashEntry next){
    this.key = key;
    this.value = value;
    this.next = next;
  }
}

我在编译时得到的唯一错误是:

error: incompatible types: Object cannot be converted to V
          return temp.value;
                     ^
  where V is a type-variable:
    V extends Object declared in class SimpleHashMap

我尝试过显式转换它,但它仍然拒绝返回 V 类型的对象。

【问题讨论】:

  • 我相信你的 HashEntry temp 应该有类型参数
  • 事实上你在 SimpleHashMap 中的所有 HashEntry 引用都应该使用 因为 HashEntry 不是 SimpleHashMap 的内部类

标签: java generics casting hashmap


【解决方案1】:

您需要使用如下类型声明您的临时变量:

HashEntry<K,V> temp = table[keyIndex];

你的get方法可以更新如下:

public V get(K key){
        int keyIndex = getHashCode(key);

        if(table[keyIndex] == null){
          return null;
        }
        else{
          HashEntry<K,V> temp = table[keyIndex];          
          while(temp != null){
            if(temp.key.equals(key)){
              return temp.value;
            }
            temp = temp.next;
          }
          return temp.value;
        }

      }

【讨论】:

    【解决方案2】:
      HashEntry temp = table[keyIndex];
    

    HashEntry 是一个泛型类型,但您使用它时没有类型信息。

    如果你想这样使用它,你必须让 HashEntry 成为一个非泛型的内部类并重用外部类的类型边界。

    public class HashEntry{ // has to be inside SimpleHashMap
      public K key; // <-- type variables from
      public V value; // <-- SimpleHashMap
      public HashEntry next;
    
      public HashEntry(K key, V value){
        this(key, value, null);
      }
    
      public HashEntry(K key, V value, HashEntry next){
        this.key = key;
        this.value = value;
        this.next = next;
      }
    }
    

    另一种可能性是保留 HashEntry 并将行更改为

      HashEntry<K, V> temp = table[keyIndex];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-21
      • 2013-05-01
      • 2013-04-13
      相关资源
      最近更新 更多