Java – how do I delete elements from a hash table using their values instead of keys?

I'm a novice in hash table. I just understand its basic operation

Hashtable<Integer,String> ht = new Hashtable<Integer,String>();
ht.put(1234,"ABCD");
ht.put(2345,"EFGH");
ht.put(4567,"IJKL");

I can use the key to delete the required elements, as shown below

System.out.println("Deleting entry with key 2345");
ht.remove(2345);
System.out.println(ht.toString());

It gives the following output

Deleting entry with key 2345
{4567=IJKL,1234=ABCD}

I can't find any way to use values as indexes and delete elements to locate elements in the hash table What should I do?

Solution

Try this

ht.values().remove("ABCD");

This will delete an entry with the specified value, which can be used if there may be multiple entries with the same value

ht.values().removeAll(Collections.singleton("ABCD"));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>