java-merging-properties
Merging java.util.Properties Objects
1. Introduction
We’ll explore three solutions, firstly starting with an example using iteration. Next, we’ll look into using the putAll() method and to conclude the tutorial, we’ll look at a more modern approach using Java 8 Streams.
To learn how to get started with Java Properties, check out our introductory article.
2. A Quick Recap on Using Properties
We typically use properties in our applications to define configuration values. In Java, we represent these values using simple key/value pairs. In addition, the key and value are both String values in each of these pairs.
Normally we use the java.util.Properties class to represent and manage these pairs of values. It is important to note that this class inherits from Hashtable.
To learn more about the Hashtable data structure read our Introduction to Java.util.Hashtable.
2.1. Setting up Properties
private Properties propertiesA() {
Properties properties = new Properties();
properties.setProperty("application.name", "my-app");
properties.setProperty("application.version", "1.0");
return properties;
}
In the above example, we create a Properties object and use the setProperty() method to set two properties. Internally, this calls the put() method from the Hashtable class but ensures the objects are String values.
Note, it is strongly discouraged to use the put() method directly as it allows the caller to insert entries whose keys or values are not Strings.
3. Merging Properties Using Iteration
private Properties mergePropertiesByIteratingKeySet(Properties... properties) {
Properties mergedProperties = new Properties();
for (Properties property : properties) {
Set<String> propertyNames = property.stringPropertyNames();
for (String name : propertyNames) {
String propertyValue = property.getProperty(name);
mergedProperties.setProperty(name, propertyValue);
}
}
return mergedProperties;
}
Let’s break this example down into steps:
-
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
-
Finally, we set the property value into the variable we created in step 1
4. Using the putAll() Method
private Properties mergePropertiesByUsingPutAll(Properties... properties) {
Properties mergedProperties = new Properties();
for (Properties property : properties) {
mergedProperties.putAll(property);
}
return mergedProperties;
}
In our second example, again we first create a Properties object to hold all our merged properties called mergedProperties. Likewise, we then iterate through the Properties objects we are going to merge but this time we add each Properties object to the mergedProperties variable using the putAll() method.
The putAll() method is another method which is inherited from Hashtable. This method allows us to copy all of the mappings from the specified Properties into our new Properties object.
It is worth mentioning that the use of putAll() with any kind of Map is also discouraged as we might end up with entries whose keys or values are not Strings
5. Merging Properties With the Stream API
private Properties mergePropertiesByUsingStreamApi(Properties... properties) {
return Stream.of(properties)
.collect(Properties::new, Map::putAll, Map::putAll);
}
In our last example, we create a Stream from our list of properties and then use the collect method to reduce the sequence of values in the stream into a new Collection. The first argument is a Supplier function used to create a new result container which in our case is a new Properties object.
The Stream API was introduced in Java 8, we have a guide on getting started with this API.
6. Conclusion
In this brief tutorial, we covered three different ways to approach merging two or more Properties objects.
As always, the examples are available in our GitHub repository.