简体   繁体   English

java.util.property的排序键集

[英]Sorting keyset of java.util.property

for some reason, I'm in a situation where I need to use a property file like: 由于某种原因,我处于需要使用以下属性文件的情况:

1=1
2=2
3=3
4=4
5=5
6=6
7=7
12=12
13=13
14=14
15=15
16=16
17=17
23=23
24=24
25=25
26=26
27=27
34=34
35=35
36=36
37=37
45=45
46=46
47=47
56=56
57=57
67=67
123=123
124=124
125=125
126=126
.................
24567=24567
34567=34567
123456=123456
123457=123457
123467=123467
123567=123567
124567=124567
134567=134567
234567=234567
1234567=1234567

And I have utility handler class to sort the keys 我有实用程序处理程序类来对键进行排序

public class PropertyHandler {

    private static PropertyHandler instance;
    private Properties properties;

    private PropertyHandler() {
        InputStream fos = null;
        try {
            fos = PropertyHandler.class.getClassLoader().getResourceAsStream("dow-pattern.properties");
            properties = new Properties() {
                @Override
                public Set<Object> keySet() {
                    return Collections.unmodifiableSet(new TreeSet<Object>(super.keySet()));
                }

                @Override
                public synchronized Enumeration<Object> keys() {
                    return Collections.enumeration(new TreeSet<Object>(super.keySet()));
                }
            };
            properties.load(fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static PropertyHandler getInstance() {
        if (instance == null) {
            instance = new PropertyHandler();
        }
        return instance;
    }

    private Properties getProperties() {
        return properties;
    }

    public static String getStringProperty(String propertyName) {
        return PropertyHandler.getInstance().getProperties().getProperty(propertyName);
    }

    public static int getIntProperty(String propertyName) {
        return Integer.parseInt(PropertyHandler.getInstance().getProperties().getProperty(propertyName));
    }

    public static Set<Object> getAllKeys() {
        return PropertyHandler.getInstance().getProperties().keySet();
}
}

But when I print the keys, by calling "getAllKeys()" the order of keys as not expected. 但是,当我打印键时,通过调用“ getAllKeys()”,键的顺序不符合预期。 It is printed in a random fashion. 它以随机方式打印。

1
12
123
1234
12345
123456
1234567
123457
12346
123467
12347
1235
12356
123567
12357
1236
........

Any pointers to solve this issue would be helpful. 解决此问题的任何指示都将有所帮助。

That's not random, that's sorted alphabetically. 这不是随机的,而是按字母顺序排序的。 You need to sort the values numerically. 您需要按数字对值进行排序。 The easiest way would be converting the Strings to Integers before adding them to the TreeSet. 最简单的方法是在将字符串添加到TreeSet之前将字符串转换为整数。

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

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