Merging java.util.Properties Objects – 合并java.util.Properties对象

最后修改: 2018年 10月 31日

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.

我们将探索三种解决方案,首先从一个使用迭代的例子开始。接下来,我们将研究使用putAll()方法,在教程的最后,我们将研究使用Java 8 Streams的更现代的方法。

To learn how to get started with Java Properties, check out our introductory article.

要了解如何开始使用 Java 属性,请查看我们的介绍性文章

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.

要了解有关Hashtable数据结构的更多信息,请阅读我们的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("", "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:


  1. First, we create a Properties object to hold all our merged properties
  2. Next, we loop over the Properties objects we are going to merge
  3. We then call the stringPropertyNames() method to get a set of property names
  4. Then we loop through all the property names and get the property value for each name
  5. 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) {
    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.

putAll() 方法是另一个从Hashtable继承的方法。该方法允许我们将指定的Properties中的所有映射复制到我们新的Properties对象中。

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

值得一提的是,对于任何类型的Map ,我们也不鼓励使用putAll(),因为我们可能最终得到键或值不是字符串的条目。

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:

最后,我们将看看如何使用Stream API来合并一个以上的Properties对象。

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.

在Java 8中引入了Stream API,我们有一份关于开始使用该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.

一如既往,这些例子可在我们的GitHub 仓库中找到。