[英]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 你必须遍历数组并删除
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.