Iterating over and deleting from Hashtable in Java

前端 未结 4 950
温柔的废话
温柔的废话 2020-12-02 16:56

I have a Hashtable in Java and want to iterate over all the values in the table and delete a particular key-value pair while iterating.

How may this be done?

4条回答
  •  庸人自扰
    2020-12-02 17:36

    You can use Enumeration:

    Hashtable table = ...
    
    Enumeration enumKey = table.keys();
    while(enumKey.hasMoreElements()) {
        Integer key = enumKey.nextElement();
        String val = table.get(key);
        if(key==0 && val.equals("0"))
            table.remove(key);
    }
    

提交回复
热议问题