更新属性实例

时间:2016-05-03 12:51:30

标签: java properties

我有Properties的实例,名为props。键值内容如下:

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

(每个键都是唯一的。)

我想要实现的目标是:

  • 我只对密钥b.<number>感兴趣,我需要b.1具有值water

  • 如果有值water但不是键b.1,而是使用键b.<x>,那么,我交换b.1&amp;的值; b.<x>

  • 如果没有值water,我会将密钥b.<number>的数字部分增加1,之后我会在"b.1" : "water"中插入Properties实例。

我开始用这段代码实现它:

// 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...
      } 
   }
}

我不确定如何在没有多次遍历属性的情况下以有效的方式实现这一点......

1 个答案:

答案 0 :(得分:0)

如果没有迭代两次(或者在下面的情况下,复制到新的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检查。