简体   繁体   English

在java中使用property.getProperty(“sample。*”)从属性文件中获取所有属性值

[英]getting all the property values from property file with property.getProperty(“sample.*”) in java

Property.properties Property.properties

sample.user = "sampleUser" sample.user =“sampleUser”
sample.age = "sampleAge" sample.age =“sampleAge”
sample.location = "sampleLocation" sample.location =“sampleLocation”

I can get a property value from a property file by prop.getProperty("sample.user"). 我可以通过prop.getProperty(“sample.user”)从属性文件中获取属性值。

I was wondering if below case is possible: 我想知道以下情况是否可能:

prop.getProperty("sample.*");

Result: 结果:
sampleUser sampleUser
sampleAge sampleAge
sampleLocation sampleLocation

Can anybody please suggest if there is any way to get the above result from the property file? 任何人都可以建议是否有任何方法可以从属性文件中获得上述结果?

One solution would be to get whole property file and iterate through it. 一种解决方案是获取整个属性文件并迭代它。 But my property file is very long and I think it would cause performance issues as I need to call it very often. 但我的属性文件很长,我认为它会导致性能问题,因为我需要经常调用它。

Anther would ve use .xml file instead of .properties file. Anther会使用.xml文件而不是.properties文件。

A Properties object (a .properties file in object form) is just a Hashtable<Object,Object> (and a Map ). Properties对象(对象形式的.properties文件)只是一个Hashtable<Object,Object> (和一个Map )。 Not ideal for any use in 2016, but perfectly workable. 不适合2016年的任何使用,但完全可行。

Extracting the matches isn't especially inefficient, and even 000s of lines should return in a trivial amount of time (potentially just a few milliseconds). 提取匹配并不是特别低效,甚至000行也应该在很短的时间内返回(可能只有几毫秒)。 It all depends how often you need to check. 这一切都取决于您需要检查的频率。 If you only need them once, just cache the resulting matchingValues and refer back to it. 如果您只需要它们一次,只需缓存生成的matchingValues并返回它。

No, you can't do prop.getProperty("sample.*"); 不,你不能做prop.getProperty("sample.*"); directly, but the code is very straightforward via the Map interface: 直接,但通过Map接口代码非常简单:

Properties p = new Properties();
p.setProperty("sample.user", "sampleUser");
p.setProperty("sample.age", "sampleAge");
p.setProperty("sample.location", "sampleLocation");

Pattern patt = Pattern.compile("sample.*");

final List<String> matchingValues = new ArrayList<>();

for (Entry<Object,Object> each : p.entrySet()) {
    final Matcher m = patt.matcher((String) each.getKey());
    if (m.find()) {
        matchingValues.add((String) each.getValue() );
    }
}

System.out.println(matchingValues);

The above matching and building took 0.16 millisecs on my 5-year-old iMac. 上述配对和建筑在我5岁的iMac上花费了0.16毫秒。

Switching to XML representation would be more complicated and definitely slower to load and process. 切换到XML表示会更复杂,加载和处理肯定更慢。

In Java 8 it may looks like Java 8中它可能看起来像

Properties p = new Properties();
...
List<String> matchingValues = p.entrySet().stream()
                .filter(e -> e.getKey().toString().matches("sample.*"))
                .map(e -> e.getValue().toString())
                .collect(Collectors.toList());

System.out.println(matchingValues);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM