如何在hashtable中put一个空值?

fzymr 2003-09-25 06:43:48
如何在hashtable中put一个空值?
...全文
198 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
swinging 2003-09-25
  • 打赏
  • 举报
回复
下面是HASHTABLE的PUT方法JDK API:
put
public Object put(Object key,
Object value)Maps the specified key to the specified value in this hashtable. Neither the key nor the value can be null.
The value can be retrieved by calling the get method with a key that is equal to the original key.


Specified by:
put in interface Map
Specified by:
put in class Dictionary
Parameters:
key - the hashtable key.
value - the value.
Returns:
the previous value of the specified key in this hashtable, or null if it did not have one.
Throws:
NullPointerException - if the key or value is null.
See Also:
Object.equals(Object), get(Object)


很明显,你的愿望不能达成,
其实要真想达到你的要求很简单,如果是空值(NULL)那么就不进行PUT操作,

就HASHMAP来说,无论是否PUT一个NULL进去,其实都是空的,取的结果一样
都是一个NULL,
所以你完全没有必要PUT一个NULL,当然,这样你需要加一个判断,
但是你用了HASHTABLE就没办法了。


最后,参数应该尽量使用接口,比如这里其实应该使用Map作为参数,
这样的话就不会有这些问题了。
fzymr 2003-09-25
  • 打赏
  • 举报
回复
但是类的接口参数是hashtable不是hashMap
kevin_c 2003-09-25
  • 打赏
  • 举报
回复
那就用HashMap呗
fzymr 2003-09-25
  • 打赏
  • 举报
回复
up

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧