2016-05-03 13 views
0

私はpropsという名前のPropertiesのインスタンスを持っています。次のようにキーと値の内容は以下のとおりです。プロパティの内容を更新するインスタンス

"a" : "apple" 
"c" : "orange" 
"b.1" : "tea" 
"b.2" : "coffee" 
"b.3" : "coke" 
... 

(各キーは一意である。)私が達成したい何

は次のとおりです。

  • 私はキーb.<number>で唯一興味を持って、I には値waterが必要です。

  • が値waterであるが、キーb.<x>で、代わりに、キーb.1でない場合には、その後、私はb.1 & b.<x>の値を入れ替えます。

  • waterがない場合、私はPropertiesインスタンスで"b.1" : "water"を挿入した後、1キーb.<number>の番号部分を増やします。

私はこのコードでそれを実装するために開始しました:

// initialize a HashMap 
Map<String, String> propMap = new HashMap<String, String>(); 

// check what are the property key-values 
Set<Object> keySet = props.keySet(); 
for (Object key : keySet) { 
    String keyStr = (String) key; 
    String valueStr = props.getProperty(key); 
    if (keyStr.startsWith("b.")) { 
     // if it is not value "water" 
     if (!valueStr.equals("water")) { 
     // I get lost... 
     } 
    } 
} 

私はプロパティを通じて複数回ループさせずに効率的な方法でこれを実装する方法がわからないです...

+0

3番目の箇条書きは、b.1 = waterを挿入し、他のすべての値を1だけ増やすことを意味しますか? – bphilipnyc

+0

@bphilipnyc、はい、正確です。 –

+0

さて、私の答えは動作します – bphilipnyc

答えて

0

私はこれを2回繰り返すことなく(または、以下の場合は新しいPropertiesオブジェクトにコピーして)これを行う方法はすぐにはわかりませんが、以下のコードが機能します。また、水が見つからない場合にのみ別のPropertiesオブジェクトを作成しますので、ユースケースによってはひどくないかもしれません。

private static Properties setUpProperties() { 

    Properties props = new Properties(); 

    props.setProperty("a", "apple"); 
    props.setProperty("c", "orange"); 
    props.setProperty("b.1", "tea"); 
    props.setProperty("b.2", "coffee"); 
    props.setProperty("b.3", "coke"); 
    return props; 
} 

@Test 
public void testProperties() { 
    @SuppressWarnings("unchecked") 
    Map<String, String> sortedMap = new TreeMap(updateContents()); //just for logging 
    System.out.print("Properties=" + sortedMap); 
} 

public static Properties updateContents() { 
    // initialize a HashMap 
    Properties props = setUpProperties(); 
    @SuppressWarnings("unchecked") 
    Map<String, String> sortedMap = new TreeMap(props); //just for logging 
    System.out.print("Properties="+sortedMap+"\n"); 

    // check what are the property key-values 
    boolean foundWater = false; 
    for (String key : props.stringPropertyNames()) { 
     if (key.startsWith("b.")) { //only b keys 
      String value = props.getProperty(key); 
      if ("water".equals(value)) { 
       foundWater = true; 
       if ("b.1".equals(key)) { 
        return props; 
       } 
       else { 
        //swap b.1 value with b.x value 
        int digit = getDigit(key); 
        props.setProperty("b." + digit, props.getProperty("b.1")); 
        props.setProperty("b.1", "water"); 
       } 
      } 
     } 
    } 
    if (foundWater) { 
     return props; 
    } 
    else { 
     Properties propertiesCopy = new Properties(); //avoid a ConcurrentModificationException 
     propertiesCopy.putAll(props); 

     for (String key : propertiesCopy.stringPropertyNames()) { 
      //increment all other b-values by one 
      if (key.startsWith("b.")) { 
       int digit = getDigit(key); 
       int incremented = digit + 1; 
       propertiesCopy.setProperty("b." + incremented, propertiesCopy.getProperty(key)); 
      } 
     } 
     propertiesCopy.setProperty("b.1", "water"); 
     return propertiesCopy; 
    } 
} 

/** 
* Returns the digit given a key from a Properties object (this is x) 
*/ 
private static int getDigit(String key) { 
    String digit = key.substring(key.lastIndexOf(".") + 1); //use period as delimiter 
    return Integer.valueOf(digit); 
} 

代わりにequalsIgnoreCaseのチェックを入れることもできます。

+0

あなたの関数 'setUpProperties()'はプロパティの内容を知らないので、forループにする必要があります。これは私の例です。したがって、合計で3つのfor-loopが必要になります。 –

+0

Hmm - ok!あなたの元の投稿からそれを知らなかったでしょう。多くの場合、あなたは.propertiesファイルを読み書きしているので、実装はこの答えの一部ではありません。 – bphilipnyc

関連する問題