1. Introduction
In this short tutorial, we’ll focus on how to merge two or more Java Properties objects into one.
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
Let’s begin by reminding ourselves of some of the key concepts of 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
To keep things simple we’re going to set up the properties programmatically for our examples:
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
Now let’s look at how we can merge two or more properties objects 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
Now we’ll look at another common solution for merging properties 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
Finally, we’ll look at how to use the Stream API to merge more than one Properties object:
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.