【问题标题】:I implemented my own hash table in java,but I need to remove object using value not key我在java中实现了自己的哈希表,但我需要使用值而不是键来删除对象
【发布时间】:2016-07-26 11:29:57
【问题描述】:

在java中自己的哈希表中,但我需要使用值而不是键编写一个函数删除对象。所以请帮助我。我还需要检查表中是否存在特定值或不在单独的函数中。

这是我的代码:

import java.util.Scanner;
class LinkedHashEntry 
{
     String key;
     int value;
     LinkedHashEntry next;

     LinkedHashEntry(String key, int value) 
     {
          this.key = key;
          this.value = value;
          this.next = null;
     }
}
class HashTable
{
     private int TABLE_SIZE;
     private int size; 
     private LinkedHashEntry[] table;

     public HashTable(int ts) 
     {
         size = 0;
         TABLE_SIZE = ts;
         table = new LinkedHashEntry[TABLE_SIZE];
         for (int i = 0; i < TABLE_SIZE; i++)
             table[i] = null;
     } 
     public int getSize()
     {
          return size;
     }
     public void makeEmpty()
     {
          for (int i = 0; i < TABLE_SIZE; i++)
              table[i] = null;
     }
     public int get(String key) 
     {
         int hash = (myhash( key ) % TABLE_SIZE);
         if (table[hash] == null)
              return -1;
         else 
         {
             LinkedHashEntry entry = table[hash];
             while (entry != null && !entry.key.equals(key))
             entry = entry.next;
             if (entry == null)
                 return -1;
             else
                  return entry.value;
         }
     }
     public void insert(String key, int value) 
     {
          int hash = (myhash( key ) % TABLE_SIZE);
          if (table[hash] == null)
              table[hash] = new LinkedHashEntry(key, value);
          else 
          {
               LinkedHashEntry entry = table[hash];
               while (entry.next != null && !entry.key.equals(key))
               entry = entry.next;
               if (entry.key.equals(key))
                     entry.value = value;
               else
                    entry.next = new LinkedHashEntry(key, value);
          }
          size++;
      }

      public void remove(String key) 
      {
          int hash = (myhash( key ) % TABLE_SIZE);
          if (table[hash] != null) 
          {
              LinkedHashEntry prevEntry = null;
              LinkedHashEntry entry = table[hash];
              while (entry.next != null && !entry.key.equals(key)) 
              {
                  prevEntry = entry;
                  entry = entry.next;
              }
              if (entry.key.equals(key)) 
              {
                  if (prevEntry == null)
                      table[hash] = entry.next;
                  else
                      prevEntry.next = entry.next;
                      size--;
              }
          }
      }
      private int myhash(String x )
      {
            int hashVal = x.hashCode( );
            hashVal %= TABLE_SIZE;
            if (hashVal < 0)
                  hashVal += TABLE_SIZE;
            return hashVal;
      }
      public void printHashTable()
      {
           for (int i = 0; i < TABLE_SIZE; i++)
           {
                   System.out.print("\nBucket "+ (i + 1) +" : ");
                   LinkedHashEntry entry = table[i];
                   while (entry != null)
                   {
                         System.out.print(entry.value +" ");
                         entry = entry.next;
                   }            
           }
      }
}
public class Hash_tab
{
     public static void main(String[] args)
     {
             Scanner scan = new Scanner(System.in);
             System.out.println("Hash Table Test\n\n");
             System.out.println("Enter size");

             HashTable ht = new HashTable(scan.nextInt() );

             char ch;

             do    
             {
                  System.out.println("\nHash Table Operations\n");
                  System.out.println("1. insert ");
                  System.out.println("2. remove");
                  System.out.println("3. get");            
                  System.out.println("4. clear");
                  System.out.println("5. size");

                  int choice = scan.nextInt();            
                  switch (choice)
                  {
                     case 1 : 
                     System.out.println("Enter key and value");
                     ht.insert(scan.next(), scan.nextInt() ); 
                     break; 

                     case 2 :                 
                     System.out.println("Enter key");
                     ht.remove( scan.next() ); 
                     break;                        

                     case 3 : 
                     System.out.println("Enter key");
                     System.out.println("Value = "+ ht.get( scan.next() )); 
                     break;                                   

                     case 4 : 
                     ht.makeEmpty();
                     System.out.println("Hash Table Cleared\n");
                     break;

                     case 5 : 
                     System.out.println("Size = "+ ht.getSize() );
                     break;         

                     default : 
                     System.out.println("Wrong Entry \n ");
                     break;   
                 }

                 ht.printHashTable();  

                 System.out.println("\nDo you want to continue (Type y or n) \n");
                 ch = scan.next().charAt(0);                        
             } while (ch == 'Y'|| ch == 'y');  
       }
}

【问题讨论】:

  • 哈希表是基于键的数据结构——值基本上与它的结构无关。如果要按值删除,则必须迭代所有条目。

标签: java hash key hashtable


【解决方案1】:

我认为您可以做的唯一有效方法是-维护&lt;value, List&lt;Keys&gt;&gt; 的另一个映射。当您需要删除任何值时,请删除与其他表中维护的所有这些键相关的条目。

否则无法从全扫描中逃脱。

【讨论】:

    【解决方案2】:

    哈希表不是这样工作的。使用其键获取(并因此删除)一个值是 Hashtables 的用途。 (我看不到你是如何实现你的哈希表的,我猜它是一个数组)。 您必须遍历数组并删除

    • 全部
    • 第一次出现

    再次:哈希表可能是错误的结构或使用错误。切换键和值(也可以用于多个值)。 您的 HashMap&lt;Integer,Integer&gt; 将是 HashMap&lt;Integer,List&lt;Integer&gt;,但您需要这样。

    PS:至少在 Java 8 中,使用内置的 Hashmap (JavaDoc) 很“容易”

    【讨论】:

    • 我明白你的意思。但我一定不会使用内置的hashmap...我需要建立自己的hash表
    猜你喜欢
    • 2014-02-18
    • 2016-03-05
    • 2012-02-29
    • 2014-03-27
    • 2018-02-08
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多