java - 我在java中实现了自己的哈希表,但是我需要使用值而不是键来删除对象

标签 java hash key hashtable

在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');  
       }
}

最佳答案

我认为唯一有效的方法是维护 <value, List<Keys>> 的另一个映射。 。当您需要删除任何值时,请删除与另一个表中维护的所有这些键相关的条目。

否则无法逃脱全面扫描。

关于java - 我在java中实现了自己的哈希表,但是我需要使用值而不是键来删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36444743/

相关文章:

Java 在新线程中打印的问题

javascript - 如何使表单操作更改哈希而不重新加载页面?

mysql - Redis 排序 : How do I sort a redis hash key by given value?

algorithm - 是否有任何采用 7 字节 key 的 DES 库或代码?

java - Swing 绘制网格。奇怪的结果

java - Java 中的 File.rename() - 它是原子操作吗?

java - 在被调用者 docker 容器之外实例化 docker 容器

html - 最佳 SRI 哈希大小是多少?

python - Pygame - 如何让我的用户更改他们的输入键? (自定义键绑定(bind))

python - 为带有祖先的 key 创建一个好看的 url