2016-04-12 14 views
0

のキーセットを並べ替え、私はのようなプロパティファイルを使用する必要がある状況にいるよ:何らかの理由java.util.property

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 

そして、私はキー

をソートするユーティリティハンドラクラスを持っています
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(); 
} 
} 

しかし、私は "getAllKeys()"を呼び出すことによって、キーを印刷するときに、キーの順序は期待されていません。ランダムな方法で印刷されます。

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

この問題を解決するためのあらゆる指針が役立ちます。

+1

ランダムに見えません...ソートされていると見なされます – 3kings

+1

値に基づいてソートするには、すべてのキーを「整数」に変換する必要があります。 –

答えて

5

これはランダムではなく、アルファベット順に並べ替えられています。値を数値でソートする必要があります。最も簡単な方法は、文字列をTreeSetに追加する前に文字列を整数に変換することです。

関連する問題