How to merge two java.util.Properties objects?


Java Problem Overview

I'm trying to have a default java.util.Properties object in my class, with the default properties it accepts, and let the developer override some of them by specifying another java.util.Properties object, but I couldn't find a nice way for doing that.

The intended usage is the following:

Properties defaultProperties = new Properties();
defaultProperties.put("key1", "value1");
defaultProperties.put("key2", "value2");

Properties otherProperties = new Properties();
otherProperties.put("key2", "value3");

Properties finalProperties = new Properties(defaultProperties);

// I'd expect to have something like:
// finalProperties.merge(otherProperties);

Java Solutions

Solution 1 - Java

java.util.Properties implements the java.util.Map interface, and so you can just treat it as such, and use methods like putAll to add the contents of another Map.

However, if you treat it like a Map, you need to be very careful with this:

new Properties(defaultProperties);

This often catches people out, because it looks like a copy constructor, but it isn't. If you use that constructor, and then call something like keySet() (inherited from its Hashtable superclass), you'll get an empty set, because the Map methods of Properties do not take account of the default Properties object that you passed into the constructor. The defaults are only recognised if you use the methods defined in Properties itself, such as getProperty and propertyNames, among others.

So if you need to merge two Properties objects, it is safer to do this:

Properties merged = new Properties();

This will give you more predictable results, rather than arbitrarily labelling one of them as the "default" property set.

Normally, I would recommend not treating Properties as a Map, because that was (in my opinion) an implementation mistake from the early days of Java (Properties should have contained a Hashtable, not extended it - that was lazy design), but the anemic interface defined in Properties itself doesn't give us many options.

Solution 2 - Java

Assuming you eventually would like to read the properties from a file, I'd go for loading both files in the same properties object like:

Properties properties = new Properties();

Solution 3 - Java

You're almost good:

Properties defaultProperties = new Properties();
defaultProperties.setProperty("key1", "value1");
defaultProperties.setProperty("key2", "value2");

Properties finalProperties = new Properties(defaultProperties);
finalProperties.setProperty("key2", "value3");

EDIT: replaced put by setProperty.

Solution 4 - Java

Yea you're right just invoke the putAll method and you're done.

Solution 5 - Java

putAll(): Copies all of the mappings from the specified map to this hashtable. These mappings will replace any mappings that this hashtable had for any of the keys currently in the specified map.

Properties merged = new Properties();

Line 2 has no effect at all. None of the properties from the first file will be in the merged properties object.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIgorView Question on Stackoverflow
Solution 1 - JavaskaffmanView Answer on Stackoverflow
Solution 2 - JavaEJBView Answer on Stackoverflow
Solution 3 - JavaJeromeView Answer on Stackoverflow
Solution 4 - JavaOscarRyzView Answer on Stackoverflow
Solution 5 - JavaVolker B.View Answer on Stackoverflow