?
Solved

Java: how would I remove the HashTable entries containing specific value?

Posted on 2012-08-15
6
Medium Priority
?
345 Views
Last Modified: 2012-08-16
Hello Experts,

For the example code below, how would I remove the Key/Value pairs for those entries containing the value "none"?

Hashtable<Integer, String> hash = new Hashtable<Integer, String>();
		hash.put(1, "value1");
		hash.put(2, "none");
		hash.put(3, "value3");
		hash.put(4, "none");
		Enumeration<Integer> e = hash.keys();
	    Object obj;
	    while (e.hasMoreElements()) {
	      obj = e.nextElement();
	      System.out.println(obj + ": " + hash.get(obj));
	    }

Open in new window


In advance, thank you for any suggestions/help...
0
Comment
Question by:epifanio67
6 Comments
 
LVL 36

Accepted Solution

by:
mccarl earned 1000 total points
ID: 38298777
Use .entrySet() rather than .keys() method of HashTable

Hashtable<Integer, String> hash = new Hashtable<Integer, String>();
		hash.put(1, "value1");
		hash.put(2, "none");
		hash.put(3, "value3");
		hash.put(4, "none");
		
                Iterator<Map.Entry<Integer, String>> e = hash.entrySet().iterator();
	        Map.Entry<Integer, String> entry;
	        while (e.hasNext()) {
	           entry = e.next();
	           System.out.println(entry.getKey() + ": " + entry.getValue());
                   if (entry.getValue().equals("none")) {
                       e.remove();
                   }
	        }

Open in new window

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1000 total points
ID: 38299617
Don't forget that Hashtable is a legacy class and should be avoided in favour of HashMap, especially if synchronization is not required and if you might need to have null values.
0
 

Author Closing Comment

by:epifanio67
ID: 38300791
Thank you experts...
I really appreciate all of your help...

Regards,
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:krakatoa
ID: 38300902
You could also do this :

Hashtable<Integer, String> hash = new Hashtable<Integer, String>();
		hash.put(1, "value1");
		hash.put(2, "none");
		hash.put(3, "value3");
		hash.put(4, "none");
	       
	        for(Map.Entry<Integer, String> entry : hash.entrySet()) {
	             
                   if (entry.getValue().equals("none")) {
                       hash.remove(entry);
		System.out.println(entry.getKey() + ": " + entry.getValue()+" was removed");
                   }
	   else{System.out.println(entry.getKey() + ": " + entry.getValue()+" was not removed");}
	        }

Open in new window


... which dispenses with an explicit Iterator.
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 38301959
It's a good thing my post came after the question was closed, because, although the code works, it's wrong, so don't use it.
0
 

Author Comment

by:epifanio67
ID: 38302428
Thank you for your help though...
I appreciate it...
Regards,
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question