【问题标题】:Sorting the properties in java在java中对属性进行排序
【发布时间】:2017-07-28 12:53:39
【问题描述】:

有没有办法在 java 中对 Properties 对象进行排序?

我有对属性进行分组并检查数据是否以地图格式提供的字符串。

【问题讨论】:

  • 创建一个TreeMap<String,String>,用您的属性填充它,然后创建一个新的Properties,并从地图中填充它...
  • 为什么还需要排序?
  • @UsagiMiyamoto 创建新的Properties 的那一刻,您将失去排序顺序。

标签: java properties


【解决方案1】:

你可以找到一个没有子类Properties和使用Java 8/9/10

的例子

通过这种方式,Properties 中的 keySetkeysentrySet 方法返回排序后的键。然后方法 store 保存也排序的属性文件。

这段代码是 here

Properties properties = new Properties() {

private static final long serialVersionUID = 1L;

@Override
public Set<Object> keySet() {
    return Collections.unmodifiableSet(new TreeSet<Object>(super.keySet()));
}

@Override
public Set<Map.Entry<Object, Object>> entrySet() {

    Set<Map.Entry<Object, Object>> set1 = super.entrySet();
    Set<Map.Entry<Object, Object>> set2 = new LinkedHashSet<Map.Entry<Object, Object>>(set1.size());

    Iterator<Map.Entry<Object, Object>> iterator = set1.stream().sorted(new Comparator<Map.Entry<Object, Object>>() {

        @Override
        public int compare(java.util.Map.Entry<Object, Object> o1, java.util.Map.Entry<Object, Object> o2) {
            return o1.getKey().toString().compareTo(o2.getKey().toString());
        }
    }).iterator();

    while (iterator.hasNext())
        set2.add(iterator.next());

    return set2;
}

@Override
public synchronized Enumeration<Object> keys() {
    return Collections.enumeration(new TreeSet<Object>(super.keySet()));
    }
};

【讨论】:

  • 欢迎提供解决方案链接,但请确保您的答案在没有它的情况下有用:add context around the link 这样您的其他用户就会知道它是什么以及为什么会出现,然后引用最相关的您链接到的页面的一部分,以防目标页面不可用。 Answers that are little more than a link may be deleted.
  • thx,除数字键外,此方法有效,即 property10 将出现在 property2 之前
【解决方案2】:

请使用这个例子:

来自链接:http://www.java2s.com/Tutorial/Java/0140__Collections/SortPropertieswhensaving.htm

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();
  }

}

【讨论】:

    【解决方案3】:

    假设 TreeMap 是一个排序的 Map,那么你可以这样做:

    //properties as they are:
    System.out.println(System.getProperties());
    
    //now sorted:
    TreeMap<Object, Object> sorted = new TreeMap<>(System.getProperties());
    System.out.println(sorted);
    

    【讨论】:

      猜你喜欢
      • 2012-05-03
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多