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
2010-02-28 14:39:32
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
2010-02-28 14:55:28
@polygenelubricants, Map is an interface that makes no restrictions on null. HashMap is a Map implementation that doesn't allow nulls.
Steve Kuo
2010-02-28 16:28:03
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
2010-02-28 19:01:32
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
2010-02-28 19:04:27
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
2010-02-28 19:14:26
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
2010-02-28 19:20:34
@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
2010-02-28 19:45:43
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
2010-02-28 19:51:48
And by the way, the code is still wrong. `getValue()` returns a `String`. A string can't be compared to the number 0.
polygenelubricants
2010-02-28 19:53:08
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
2010-02-28 14:42:49
You can do this but it's not necessary to use an additional collection; It just makes things more complicated.
Adamski
2010-02-28 19:05:06
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
2010-02-28 14:47:41