小编Per*_*ohn的帖子

关于在Java中实现我自己的HashMap的问题

我正在进行一项任务,我必须实现自己的HashMap.在赋值文本中,它被描述为一个列表数组,每当你想要添加一个元素时,它最终在数组中的位置由其hashCode决定.在我的例子中,它是电子表格中的位置,所以我刚刚使用了columnNumber + rowNumber,然后将其转换为String,然后转换为int,作为hashCode,然后我将其插入到Array中.它当然以节点(键,值)的形式插入,其中键是单元格的位置,值是单元格的值.

但我必须说我不明白为什么我们需要一个列表数组,因为如果我们最终得到一个包含多个元素的列表,它会不会相当大地增加查找时间?那么它不应该是一个节点数组吗?

我也发现了Java中HashMap的这种实现:

public class HashEntry {
      private int key;
      private int value;

      HashEntry(int key, int value) {
            this.key = key;
            this.value = value;
      }     

      public int getKey() {
            return key;
      }

      public int getValue() {
            return value;
      }
}

public class HashMap {
  private final static int TABLE_SIZE = 128;

  HashEntry[] table;

  HashMap() {
        table = new HashEntry[TABLE_SIZE];
        for (int i = 0; i < TABLE_SIZE; i++)
              table[i] = null;
  }

  public int get(int key) {
        int …
Run Code Online (Sandbox Code Playgroud)

java hashmap

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

hashmap ×1

java ×1