views:

566

answers:

3

I have a hashtable in java and want to iterate over all the value in the table and delete a particular key,value pair while iterating. Is there some way i could do that

+3  A: 

You need to use an explicit java.util.Iterator to iterate over the Map's entry set rather than being able to use the enhanced For-loop syntax available in Java 6. The following example iterates over a Map of Integer, String pairs, removing any entry whose Integer key is null or equals 0.

Map<Integer, String> map = ...

Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();

while (it.hasNext()) {
  Map.Entry<Integer, String> entry = it.next();

  // Remove entry if key is null or equals 0.
  if (entry.getKey() == null || entry.getKey() == 0) {
    it.remove();
  }
}
Adamski
You can't have `null` keys in a `Hashtable`. That's what makes it different from a `Map`. Also, you're querying `Map.Entry getValue` in the above code, instead of `getKey`. You can't do `entry.getValue() == 0` because the values are of type `String`.
polygenelubricants
@polygenelubricants, Map is an interface that makes no restrictions on null. HashMap is a Map implementation that doesn't allow nulls.
Steve Kuo
I don't reference Hashtable in my code and would not use it anyway as it's superseded by HashMap (which **does** allow null keys and values).
Adamski
BTW 2 downvotes?! Seems like some people are stuck using JDK 1.1 collections or just don't bother reading the API definition of Map.
Adamski
I mention `Hashtable` because that's what OP has. And @Steve, `HashMap` does allow nulls (http://java.sun.com/javase/6/docs/api/java/util/HashMap.html).
polygenelubricants
And @Adamski, people are downvoting because the code is wrong. You said your code is supposed to "remove entry if key is null or equals 0", but instead you query `entry.getValue()` (which is of `String` type) instead of `entry.getKey()` (which is of Integer type).
polygenelubricants
@poly: Fair point and thanks for pointing that out (corrected) but why not just add a comment to that effect rather than downvote (which doesn't help anyone)?
Adamski
The first comment in this answer is mine, and it said exactly that. As for why people downvote instead of commenting, you have to ask them.
polygenelubricants
And by the way, the code is still wrong. `getValue()` returns a `String`. A string can't be compared to the number 0.
polygenelubricants
A: 

You can use a temporary deletion list:

List<String> keyList = new ArrayList<String>;

for(Map.Entry<String,String> entry : hashTable){
  if(entry.getValue().equals("delete")) // replace with your own check
    keyList.add(entry.getKey());
}

for(String key : keyList){
  hashTable.remove(key);
}

You can find more information about Hashtable methods in the Java API

Scharrels
You can do this but it's not necessary to use an additional collection; It just makes things more complicated.
Adamski
A: 

So you know the key, value pair that you want to delete in advance? It's just much clearer to do this, then:

 table.delete(key);
 for (K key: table.keys()) {
    // do whatever you need to do with the rest of the keys
 }
polygenelubricants
i only knew the value so this wont work.Thanks though for looking
Mohit BAnsal