1. Introduction
1.介绍
In this short tutorial, we’ll focus on how to merge two or more Java Properties objects into one.
在这个简短的教程中,我们将重点讨论如何将两个或多个JavaProperties对象合并成一个。
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
2.关于使用属性的快速回顾
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.
我们通常在我们的应用程序中使用属性来定义配置值。在Java中,我们使用简单的键/值对来表示这些值。此外,在这些对中,键和值都是String值。
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.
通常,我们使用java.util.Properties类来表示和管理这些值对。需要注意的是,这个类继承于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
2.1.设置属性
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.
在上面的例子中,我们创建了一个Properties对象并使用setProperty()方法来设置两个属性。在内部,这调用了Hashtable类中的put()方法,但确保对象是String值。
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.
注意,强烈不建议直接使用put()方法,因为它允许调用者插入键或值不是字符串的条目。
3. Merging Properties Using Iteration
3.用迭代法合并属性
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
4.使用putAll()方法
Now we’ll look at another common solution for merging properties using the putAll() method:
现在我们来看看另一个使用putAll()方法来合并属性的常见解决方案。
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.
在第二个例子中,我们首先创建一个Properties对象来保存我们所有合并的属性,称为mergedProperties。同样地,我们然后遍历我们要合并的Properties对象,但这次我们使用putAll()方法将每个Properties对象添加到mergedProperties变量。
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
5.使用流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.
在我们的最后一个例子中,我们从我们的属性列表中创建了一个Stream,然后使用collect方法将流中的值序列减少到一个新的Collection。第一个参数是一个Supplier函数,用于创建一个新的结果容器,在我们的例子中,它是一个新的Properties对象。
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
6.结论
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 仓库中找到。