Properties
先看代码:
public
class Properties extends Hashtable<Object,Object> {
/**
* use serialVersionUID from JDK 1.1.X for interoperability
*/
@java.io.Serial
private static final long serialVersionUID = 4112578634029874840L;
private static final Unsafe UNSAFE = Unsafe.getUnsafe();
/**
* A property list that contains default values for any keys not
* found in this property list.
*
* @serial
*/
protected volatile Properties defaults;
/**
* Properties does not store values in its inherited Hashtable, but instead
* in an internal ConcurrentHashMap. Synchronization is omitted from
* simple read operations. Writes and bulk operations remain synchronized,
* as in Hashtable.
*/
private transient volatile ConcurrentHashMap<Object, Object> map;
//省略....
}
```
可以看到,Properties 是继承 Hashtable. 不过。他有两个属性:Properties defaults 和ConcurrentHashMap<Object, Object> map。
```java
/**
* Calls the {@code Hashtable} method {@code put}. Provided for
* parallelism with the {@code getProperty} method. Enforces use of
* strings for property keys and values. The value returned is the
* result of the {@code Hashtable} call to {@code put}.
*
* @param key the key to be placed into this property list.
* @param value the value corresponding to {@code key}.
* @return the previous value of the specified key in this property
* list, or {@code null} if it did not have one.
* @see #getProperty
* @since 1.2
*/
public synchronized Object setProperty(String key, String value) {
return put(key, value);
}
@Override
public synchronized Object put(Object key, Object value) {
return map.put(key, value);
}
/**
* Searches for the property with the specified key in this property list.
* If the key is not found in this property list, the default property list,
* and its defaults, recursively, are then checked. The method returns
* {@code null} if the property is not found.
*
* @param key the property key.
* @return the value in this property list with the specified key value.
* @see #setProperty
* @see #defaults
*/
public String getProperty(String key) {
Object oval = map.get(key);
String sval = (oval instanceof String) ? (String)oval : null;
Properties defaults;
return ((sval == null) && ((defaults = this.defaults) != null)) ? defaults.getProperty(key) : sval;
}
```
可以看到,加入新值的时候,是先存放到map里面的。也就时说,Properties虽然继承了Hashtable.但并不是新加的值都放到defaults里面的,而是放到 private