Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two java.util.Properties objects?

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); // 
like image 367
Igor Avatar asked Jan 05 '10 08:01

Igor


People also ask

How do I combine two properties in Java?

First, we create a Properties object to hold all our merged properties. Next, we loop over the Properties objects we are going to merge. We then call the stringPropertyNames() method to get a set of property names. Then we loop through all the property names and get the property value for each name.

How do I combine multiple objects into one in Java?

Object [] combined = ObjectArrays. concat(first, second, Object. class); It can be used with different data types, and it accepts two source arrays along with the class literal to return the combined array.

How do you combine a list of objects in Java?

One way to merge multiple lists is by using addAll() method of java. util. Collection class, which allows you to add the content of one List into another List. By using the addAll() method you can add contents from as many List as you want, it's the best way to combine multiple List.


2 Answers

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(); merged.putAll(properties1); merged.putAll(properties2); 

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.

like image 73
skaffman Avatar answered Oct 04 '22 14:10

skaffman


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(); properties.load(getClass().getResourceAsStream("default.properties")); properties.load(getClass().getResourceAsStream("custom.properties")); 
like image 20
EJB Avatar answered Oct 04 '22 15:10

EJB