2012-04-23 59 views
14

我有一個屬性變量,有時我需要添加其他屬性。如何在java中排序屬性?

Properties myBasicProps = this.getClass.getResourceAsStream(MY_PROPS_PATH); 
... 
Properties otherProps = new Properties(); 
otherProps.load(new StringReader(tempPropsString)); //tempPropsString contains my temporary properties 
myBasicProps.putAll(otherProps); 

我想在這之後對myBasicProps進行排序。我不想獲得所有的鍵和值,用Collections.sort()對它們進行排序,然後將它們全部放到一個新的對象中。有沒有更好的辦法?

+1

http://www.rgagnon.com/javadetails/java-0614.html – nayakam 2012-04-23 06:46:36

+3

@Thillakan我貼上相同的鏈接,但看完後刪除了「我不想獲取所有的鍵和值,用Collections.sort()對它們進行排序...「 – 2012-04-23 06:48:27

回答

19

不,java.util.Properties延伸java.util.Hashtable它沒有定義鍵或值的可預測的排序順序。

您可以嘗試將所有值轉儲到java.util.TreeMap之類的東西,這會對您的密鑰施加自然排序。

+1

這將意味着大寫先小寫。 – Artem 2013-11-08 05:41:54

15

您所要做的就是創建擴展屬性的類。 來源:java2s.com

import java.io.FileOutputStream; 
import java.util.Collections; 
import java.util.Enumeration; 
import java.util.Properties; 
import java.util.Vector; 

public class Main{ 
    public static void main(String[] args) throws Exception { 
    SortedProperties sp = new SortedProperties(); 
    sp.put("B", "value B"); 
    sp.put("C", "value C"); 
    sp.put("A", "value A"); 
    sp.put("D", "value D"); 
    FileOutputStream fos = new FileOutputStream("sp.props"); 
    sp.store(fos, "sorted props"); 
    } 

} 
class SortedProperties extends Properties { 
    public Enumeration keys() { 
    Enumeration keysEnum = super.keys(); 
    Vector<String> keyList = new Vector<String>(); 
    while(keysEnum.hasMoreElements()){ 
     keyList.add((String)keysEnum.nextElement()); 
    } 
    Collections.sort(keyList); 
    return keyList.elements(); 
    } 

} 

它爲我工作。

+2

Java 8版本:ArrayList result = Collections.list(super.keys()); Collections.sort(result,(a,b) - > a.toString()。compareTo(b.toString())); return Collections.enumeration(result); – user2069723 2016-03-31 15:44:29

2

簡單得多,只是按鍵排序:

List<String> keys = new ArrayList<String>() 
for(String key : properties.stringPropertyNames()) { 
    keys.add(key) 
} 

Collections.sort(keys); 
5

@ danisupr4有最好的解決方案。

我會稍微提高,這樣你就不會在你的IDE收到任何警告:

public static class MyProperties extends Properties { 
    private static final long serialVersionUID = 1L; 

    public Enumeration<Object> keys() { 
     Enumeration<Object> keysEnum = super.keys(); 
     Vector<Object> keyList = new Vector<Object>(); 

     while (keysEnum.hasMoreElements()) { 
      keyList.add(keysEnum.nextElement()); 
     } 

     Collections.sort(keyList, new Comparator<Object>() { 
      @Override 
      public int compare(Object o1, Object o2) { 
       return o1.toString().compareTo(o2.toString()); 
      } 
     }); 

     return keyList.elements(); 
    } 
} 
2

可以覆蓋在txt文件保存排序屬性鍵()方法:

//this method downloaded from edu.umd.cs.findbugs.config package 
@SuppressWarnings("unchecked") 
@Override 
public synchronized Enumeration<Object> keys() { 
    Set<?> set = keySet(); 
    return (Enumeration<Object>) sortKeys((Set<String>) set); 
} 
static public Enumeration<?> sortKeys(Set<String> keySet) { 
    List<String> sortedList = new ArrayList<String>(); 
    sortedList.addAll(keySet); 
    Collections.sort(sortedList); 
    return Collections.enumeration(sortedList); 
} 

並重寫stringPropertyNames()方法用於xml文件保存排序屬性:

@Override 
public Set<String> stringPropertyNames() { 
Set<String> tmpSet = new TreeSet<String>(); 
for (Object key : keySet()) 
{ 
    tmpSet.add(key.toString()); 
} 
    return tmpSet; 
} 
3

TreeMap中應該是最簡單的方法:

Properties myProps = this.getClass.getResourceAsStream(MY_PROPS_PATH); 

try { 
    myProps.load(new FileInputStream(extraPropertiesFilename)); 
     //you can load more properties from external file 

    Map<String, String> sortedMap = new TreeMap(myProps); 

    //output sorted properties (key=value) 
    for (String key : sortedMap.keySet()) { 
     System.out.println(key + "=" + sortedMap.get(key)); 
    } 

} catch (Exception e) { 
    e.printStackTrace(); 
}