Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

Remove key based on value in Hashtable

Thread view: 
bunallo - 26 Feb 2005 01:12 GMT
I have a Hashtable that never contains duplicate values. The problem is that
I in a class only have access to the values of a Hashtable an not the keys.
I would like to be able to removed keys in this Hashtable based on values
and not keys is that possible??
Anthony Borla - 26 Feb 2005 05:32 GMT
> I have a Hashtable that never contains duplicate values.
> The problem is that in a class only have access to the
> values of a Hashtable an not the keys. I would like to
> be able to removed keys in this Hashtable based on
> values and not keys is that possible??

It is possible to do as [what I think] you require: obtain a Collection
object via the 'values' method, get an Iterator object [via the 'iterator'
method], and remove the underlying key / value entry via the iterator.

Note, however, that you do not have access to the key, and you've
effectively bypassed the 'key handling' methods of the Hashtable. I'm not
sure that this is the best use of a Hashtable, however I realise that this
is a somewhat ad hoc requirement.

The code below provides a quick example.

I hope this helps.

Anthony Borla

// --------------------

import java.util.*;

public class HashTest
{
 public static void main(String[] args)
 {
   Hashtable numbers = new Hashtable();

   numbers.put("one", new Integer(1));
   numbers.put("two", new Integer(2));
   numbers.put("three", new Integer(3));

   System.out.println("Size: " + numbers.size());

   Iterator nci = numbers.values().iterator();

   while (nci.hasNext())
     System.out.println(nci.next());

   nci = numbers.values().iterator();

   while (nci.hasNext())
   {
     if (((Integer)nci.next()).intValue() == 2)
       nci.remove();
   }

   System.out.println("Size: " + numbers.size());
 }
}
bunallo - 26 Feb 2005 11:45 GMT
> > I have a Hashtable that never contains duplicate values.
> > The problem is that in a class only have access to the
[quoted text clipped - 49 lines]
>   }
> }

Thank you very much that was just what I needed to do!
Paul Chapman - 26 Feb 2005 10:47 GMT
"bunallo" wote:

> I have a Hashtable that never contains duplicate values. The problem is that
> I in a class only have access to the values of a Hashtable an not the keys.
> I would like to be able to removed keys in this Hashtable based on values
> and not keys is that possible??

My recommendation: build your own OneToOneMap class which maintains a
key->value hashtable and a value->key hashtable.  On average, it'll have
half the performance, but removing-by-value will be (nearly) constant-time.

Cheers, Paul


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.