简体   繁体   English

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

[英]I implemented my own hash table in java,but I need to remove object using value not key

In own hash table in java,but I need to write a function remove object using value not key.So please help me.And I also need to check a particular value exists in the table or not in a separate function. 在java中自己的哈希表中,但是我需要使用值而不是key来编写函数remove对象。所以请帮助我。我还需要检查表中是否存在特定值或不在单独的函数中。

Here is my code: 这是我的代码:

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

I think the only efficient way you can do is- maintain another mapping of <value, List<Keys>> . 我认为唯一有效的方法是 - 维护<value, List<Keys>>另一个映射。 When you need to remove any value, remove the entries related to all those keys maintained in the other table. 当您需要删除任何值时,请删除与另一个表中维护的所有这些键相关的条目。

Otherwise there is no escape from full scan. 否则完全扫描无法逃脱。

Hashtables do not work that way. 哈希表不会那样工作。 Getting (and therefore deleting) a value with its key is what Hashtables are for. 使用密钥获取(并因此删除)值是Hashtables的用途。 (I am unable to see how you implemented your Hashtable, I guess it is an array). (我无法看到你如何实现你的Hashtable,我猜它是一个数组)。 You have to iterate through the array and delete 你必须遍历数组并删除

  • all 所有
  • the first occurrence 第一次出现

Again: A Hashtable might be either the wrong Structure or used false. 再次:Hashtable可能是错误的结构或使用false。 Either Switch key and value (which is possible for multiple values as well). 切换键和值(也可以是多个值)。 Your HashMap<Integer,Integer> will be a HashMap<Integer,List<Integer> , but as you need it that way. 你的HashMap<Integer,Integer>将是一个HashMap<Integer,List<Integer> ,但是你需要这样。

PS: At least in Java 8 it is 'easy' to do with the built-in Hashmap ( JavaDoc ) PS:至少在Java 8中,使用内置的Hashmap( JavaDoc )很容易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我是否需要定义自己的哈希和相等的方法? - Whether I need to define my own hash and equal method? 我需要创建自己的哈希和相等方法吗? - Do I need to create my own hash and equal methods? 如果我不想将我的对象用作哈希表中的键,为什么两个相等的对象应返回相等的哈希码? - Why should two equal objects return equal hash codes if I don't want to use my object as a key in a hash table? 尝试使用string作为键和double作为值在java中创建哈希表 - Trying to create a hash table in java using string as key & double as value Java弱哈希映射 - 需要根据值的弱点而不是键来删除条目 - Java Weak Hash Map - Need to remove entry based on weakness of value, not key 如果我不使用Java中的对象使用哈希表或哈希集,是否需要实现hashcode() - do I need to have hashcode() implemented if I dont use hashtables or hashset with my objects in java 我需要重命名我在 java 中生成的 json object 的密钥 - I need to rename the key of the json object that I generate in java 我如何在java中显示哈希表的内容 - How I display the content of my hash table in java Java AES并使用我自己的密钥 - Java AES and using my own Key 将本机Java对象映射到自己的实现类 - Mapping native java Object to own implemented class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM