java properties map_为什么java.util.Properties实现Map而不是Map

java.util.Properties类用于表示键和值都是字符串的映射.这是因为Properties对象用于读取.properties文件,这些文件是文本文件.

那么,为什么在Java 5中他们改进了这个类来实现Map< Object,Object>而不是Map< String,String>?

Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead. If the store or save method is called on a “compromised” Properties object that contains a non-String key or value, the call will fail.

由于键和值都应该是字符串,那么为什么不使用正确的泛型类型静态强制执行呢?

我想使Properties实现Map< String,String>将不会完全向后兼容为Java 5之前编写的代码.如果您有旧的代码将非字符串粘贴到Properties对象中,那么该代码将不再使用Java 5编译.但是……这不是一个好的事情?在编译时捕获这种类型错误不是泛型的全部意义吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值