java.util.Propertiesオブジェクトのマージ

java.util.Propertiesオブジェクトのマージ

1. 前書き

この短いチュートリアルでは、we’ll focus on how to merge two or more Java Properties objects into one.

最初に反復を使用した例から始めて、3つのソリューションを検討します。 次に、putAll()メソッドの使用を検討し、チュートリアルを終了するために、Java8ストリームを使用したより最新のアプローチを検討します。

Javaプロパティの使用を開始する方法については、introductory articleを確認してください。

2. プロパティの使用に関する簡単な要約

まず、プロパティの重要な概念のいくつかを思い出してみましょう。

We typically use properties in our applications to define configuration values。 Javaでは、単純なキー/値のペアを使用してこれらの値を表します。 さらに、キーと値は両方とも、これらの各ペアのString値です。

通常、これらの値のペアを表し、管理するためにjava.util.Propertiesクラスを使用します。 It is important to note that this class inherits from Hashtable.

Hashtableのデータ構造の詳細については、Introduction to Java.util.Hashtableをご覧ください。

2.1. プロパティの設定

簡単にするために、例のプロパティをプログラムで設定します。

private Properties propertiesA() {
    Properties properties = new Properties();
    properties.setProperty("application.name", "my-app");
    properties.setProperty("application.version", "1.0");
    return properties;
}

上記の例では、Propertiesオブジェクトを作成し、setProperty()メソッドを使用して2つのプロパティを設定します。 内部的には、これはHashtableクラスからput()メソッドを呼び出しますが、オブジェクトがString値であることを保証します。

呼び出し元がキーまたは値がStringsでないエントリを挿入できるため、put()メソッドを直接使用することは強くお勧めしません。

3. 反復を使用したプロパティのマージ

次に、反復を使用して2つ以上のプロパティオブジェクトをマージする方法を見てみましょう。

private Properties mergePropertiesByIteratingKeySet(Properties... properties) {
    Properties mergedProperties = new Properties();
    for (Properties property : properties) {
        Set propertyNames = property.stringPropertyNames();
        for (String name : propertyNames) {
            String propertyValue = property.getProperty(name);
            mergedProperties.setProperty(name, propertyValue);
        }
    }
    return mergedProperties;
}

この例をいくつかのステップに分けてみましょう。

  1. まず、マージされたすべてのプロパティを保持するPropertiesオブジェクトを作成します

  2. 次に、マージするPropertiesオブジェクトをループします

  3. 次に、stringPropertyNames() methodを呼び出して、プロパティ名のセットを取得します

  4. 次に、すべてのプロパティ名をループし、各名前のプロパティ値を取得します

  5. 最後に、手順1で作成した変数にプロパティ値を設定します

4. putAll()メソッドの使用

次に、putAll()メソッドを使用してプロパティをマージするための別の一般的なソリューションを見ていきます。

private Properties mergePropertiesByUsingPutAll(Properties... properties) {
    Properties mergedProperties = new Properties();
    for (Properties property : properties) {
        mergedProperties.putAll(property);
    }
    return mergedProperties;
}

2番目の例でも、最初にPropertiesオブジェクトを作成して、mergedPropertiesと呼ばれるすべてのマージされたプロパティを保持します。 同様に、マージするPropertiesオブジェクトを反復処理しますが、今回は、putAll()メソッドを使用して各PropertiesオブジェクトをmergedProperties 変数に追加します。

putAll()メソッドは、Hashtableから継承される別のメソッドです。 This method allows us to copy all of the mappings from the specified Properties into our new Properties object.

キーまたは値がStringsでないエントリが作成される可能性があるため、putAll() を任意の種類のMap isで使用することもお勧めしません。

5. プロパティとStreamAPIのマージ

最後に、Stream APIを使用して複数のPropertiesオブジェクトをマージする方法を見ていきます。

private Properties mergePropertiesByUsingStreamApi(Properties... properties) {
    return Stream.of(properties)
        .collect(Properties::new, Map::putAll, Map::putAll);
}

最後の例では、プロパティのリストからStream を作成し、collect メソッドを使用して、ストリーム内の値のシーケンスを新しいCollectionに減らします。 The first argument is a Supplier function used to create a new result container which in our case is a new Properties object.

StreamAPIはJava8で導入されました。このAPIを使用したgetting startedに関するガイドがあります。

6. 結論

この簡単なチュートリアルでは、2つ以上のPropertiesオブジェクトをマージするための3つの異なる方法について説明しました。

いつものように、例はGitHub repositoryで利用できます。