我在java中实现了自己的哈希表,但我需要使用value not key删除对象

时间:2016-04-06 07:50:54

标签: java hash key hashtable

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

这是我的代码:

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

2 个答案:

答案 0 :(得分:0)

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

否则完全扫描无法逃脱。

答案 1 :(得分:0)

哈希表不会那样工作。使用密钥获取(并因此删除)值是Hashtables的用途。 (我无法看到你如何实现你的Hashtable,我猜它是一个数组)。 你必须遍历数组并删除

  • 所有
  • 第一次出现

再次:Hashtable可能是错误的结构或使用false。切换键和值(也可以是多个值)。 您的HashMap<Integer,Integer>将是HashMap<Integer,List<Integer>,但正如您所需要的那样。

PS:至少在Java 8中它很容易&#39;与内置的Hashmap(here in the injectServiceLocator method at line 208 in ControllerManager.php

有关