一种保持顺序的Properties

其实properties有没有顺序都一样 程序都能正常运行

但看着就比较闹心 所以网上找了找 还真有人给了个例子实现读Property的有序

但是删除某些属性之后 写入又有问题 会异常 后来重写了一下remove方法 

目前貌似没有问题了

 

import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Properties;
import java.util.Set;

public class OrderedProperties extends Properties {

private static final long serialVersionUID = -4627607243846121965L;

private final LinkedHashSet<Object> keys = new LinkedHashSet<Object>();
public Enumeration<Object> keys() {
return Collections.<Object> enumeration(keys);
}

public Object put(Object key, Object value) {
keys.add(key);
return super.put(key, value);
}

public Object remove(Object key){
keys.remove(key);
return super.remove(key);
}

public Set<Object> keySet() {
return keys;
}

public Set<String> stringPropertyNames() {
Set<String> set = new LinkedHashSet<String>();

for (Object key : this.keys) {
set.add((String) key);
}

return set;
}

}

 

参考原文:http://blog.csdn.net/happylee6688/article/details/46894131/

posted @ 2016-05-19 10:20  boiledwater  阅读(351)  评论(0编辑  收藏  举报