当我尝试在哈希映射中输入新密钥时,如何计算比较?

时间:2016-05-03 12:13:00

标签: java dictionary hash hashmap hashtable

我想创建一个方法来计算当我想在哈希映射中放入一个新的随机密钥时进行多少次比较。我用来在地图中放置新密钥的代码如下:

public void put(int key, int value) { 
          int hash = (key % table.length);
          int initialHash = -1;
          int indexOfDeletedEntry = -1;

          while (hash != initialHash
                      && (table[hash] == DeletedEntry.getUniqueDeletedEntry()
                      || table[hash] != null
                      && table[hash].getKey() != key)) {
                if (initialHash == -1)
                      initialHash = hash;
                if (table[hash] == DeletedEntry.getUniqueDeletedEntry())
                      indexOfDeletedEntry = hash;
                hash = (hash + 1) % table.length;
          }
          if ((table[hash] == null || hash == initialHash)
                      && indexOfDeletedEntry != -1) {
                table[indexOfDeletedEntry] = new HashEntry(key, value);
                size++;
          } else if (initialHash != hash)
                if (table[hash] != DeletedEntry.getUniqueDeletedEntry()
                           && table[hash] != null && table[hash].getKey() == key)
                      table[hash].setValue(value);
                else {
                      table[hash] = new HashEntry(key, value);
                      size++;
                }
          if (size >= maxSize)
                resize();
    }

已删除条目的类如下:

public class DeletedEntry extends HashEntry {

    private static DeletedEntry entry = null;

    private DeletedEntry() {
          super(-1, -1);
    }

    public static DeletedEntry getUniqueDeletedEntry() {
          if (entry == null)
                entry = new DeletedEntry();
          return entry;
    }

}

另外,HashEntry类有2个int变量,int key和int value。 任何想法如何计算比较? 这就是我在我的主要工作中所做的:

Random rand = new Random();
        int[] comparisons = new int[20]; 

        int key = 0;

        for (int k=0;k<20;k++){
            key = rand.nextInt(1000) + 1; 
        }

2 个答案:

答案 0 :(得分:2)

(我假设这是某种学习练习。因此,使用或扩展现有Map实施的建议是无关紧要的。)

简单的答案是,每次“比较”键时都会增加一个计数器。你可以内联,或者你可以写一个像这样的小帮手方法:

  private boolean compareKeys(int key1, int key2) {
      count++;
      return key1 == key2;
  }

然后更改代码以在每次比较密钥时使用此帮助程序; e.g。

  while (hash != initialHash
              && (table[hash] == DeletedEntry.getUniqueDeletedEntry()
              || table[hash] != null
              && !compareKeys(table[hash].getKey(), key))) {

  if (table[hash] != DeletedEntry.getUniqueDeletedEntry()
       && table[hash] != null 
       && compareKeys(table[hash].getKey(), key))

这个问题确实没有聪明的解决方法。

答案 1 :(得分:1)

您可以自己编写CustomHashMap

在这个CustomHashMap中,您可以实现一个新的put()方法,该方法会对比较进行计数,然后返回该值。

public int put(int key, int value) { 
          int hash = (key % table.length);
          int initialHash = -1;
          int indexOfDeletedEntry = -1;
          int numberOfComparisons = 1;

          while (hash != initialHash
                      && (table[hash] == DeletedEntry.getUniqueDeletedEntry()
                      || table[hash] != null
                      && table[hash].getKey() != key)) {
                numberOfComparisons++;
                if (initialHash == -1)
                      initialHash = hash;
                if (table[hash] == DeletedEntry.getUniqueDeletedEntry())
                      indexOfDeletedEntry = hash;
                hash = (hash + 1) % table.length;
          }
          if ((table[hash] == null || hash == initialHash)
                      && indexOfDeletedEntry != -1) {
                table[indexOfDeletedEntry] = new HashEntry(key, value);
                size++;
          } else if (initialHash != hash)
                if (table[hash] != DeletedEntry.getUniqueDeletedEntry()
                           && table[hash] != null && table[hash].getKey() == key)
                      table[hash].setValue(value);
                else {
                      table[hash] = new HashEntry(key, value);
                      size++;
                }
          if (size >= maxSize)
                resize();
          return numberOfComparisons;
 }