Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>java.util.Properties</code> implements the <code>java.util.Map</code> interface, and so you <em>can</em> just treat it as such, and use methods like <code>putAll</code> to add the contents of another <code>Map</code>.</p> <p>However, if you treat it like a Map, you need to be very careful with this:</p> <pre><code>new Properties(defaultProperties); </code></pre> <p>This often catches people out, because it <em>looks</em> like a copy constructor, but it isn't. If you use that constructor, and then call something like <code>keySet()</code> (inherited from its <code>Hashtable</code> superclass), you'll get an empty set, because the <code>Map</code> methods of <code>Properties</code> do not take account of the default <code>Properties</code> object that you passed into the constructor. The defaults are only recognised if you use the methods defined in <code>Properties</code> itself, such as <code>getProperty</code> and <code>propertyNames</code>, among others.</p> <p>So if you need to merge two Properties objects, it is safer to do this:</p> <pre><code>Properties merged = new Properties(); merged.putAll(properties1); merged.putAll(properties2); </code></pre> <p>This will give you more predictable results, rather than arbitrarily labelling one of them as the "default" property set.</p> <p>Normally, I would recommend not treating <code>Properties</code> as a <code>Map</code>, because that was (in my opinion) an implementation mistake from the early days of Java (Properties should have contained a <code>Hashtable</code>, not extended it - that was lazy design), but the anemic interface defined in <code>Properties</code> itself doesn't give us many options.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload