Properties
Properties
类继承自HashTable
类并且实现了Map接口,也是使用键值对的形式来存储数据,来表示一个持久的属性集。Properties
的特点和HashTable
类似。Properties
还可以用于从xxx.properties
文件,加载数据到Properties
类对象,并进行读取和修改。该类也被许多Java类使用,比如获取系统属性时,System.getProperties
方法就是返回一个Properties
对象。
构造方法
-
public Properties()
:创建一个空的属性列表。
常用方法
基本的存储方法
-
public Object setProperty(String key, String value)
:保存一对属性。 -
public String getProperty(String key)
:使用此属性列表中指定的键搜索属性值。 -
public Set<String> stringPropertyNames()
:所有键的名称的集合。 -
public void list(PrintStream out)
:将数据显示到指定设备
public static void main(String[] args) throws FileNotFoundException {
// 创建属性集对象
Properties properties = new Properties();
// 添加键值对元素
properties.setProperty("filename", "a.txt");
properties.setProperty("length", "209385038");
properties.setProperty("location", "D:\\a.txt");
// 打印属性集对象
System.out.println(properties);
// 通过键,获取属性值
System.out.println(properties.getProperty("filename"));
System.out.println(properties.getProperty("length"));
System.out.println(properties.getProperty("location"));
// 遍历属性集,获取所有键的集合
Set<String> strings = properties.stringPropertyNames();
// 打印键值对
for (String key : strings ) {
System.out.println(key+" -- "+properties.getProperty(key));
}
// list方法,把属性全部输出到控制台
properties.list(System.out);
}
与流相关的方法
-
public void load(InputStream inStream)
:从字节输入流中读取键值对。 -
public void store(OutputStream out, String comments)
:将Properties中的键值对存储到配置文件,在idea中,如果有中文,会存储为Unicode码。
参数中使用了字节输入流,通过流对象,可以关联到某文件上,这样就能够加载文本中的数据了。
文本中的数据,必须是键值对形式,可以使用空格、等号、冒号等符号分隔。
public static void main(String[] args) throws FileNotFoundException {
// 创建属性集对象
Properties pro = new Properties();
// 加载文本中信息到属性集
pro.load(new FileInputStream("read.txt"));
// 遍历集合并打印
Set<String> strings = pro.stringPropertyNames();
for (String key : strings ) {
System.out.println(key+" -- "+pro.getProperty(key));
}
// 保存到新的文件, null表示的是注释
pro.store(new FileOutputStream("src\\read2.txt"), null);
}
① Properties的Key和Value都不能为
null
,因为Properties继承了HashTable。
② 如果有相同的Key,那么Value会被替换。
TreeSet
TreeSet实现了Set接口,是一个有序的集合。它继承了AbstractSet
抽象类,实现了NavigableSet<E>
,Cloneable
,Serializable接口。TreeSet
是基于TreeMap
实现的,TreeSet的元素支持2种排序方式:自然排序或者根据提供的Comparator进行排序。
使用方式和Set集合差不多
自然排序是默认的,也可以使用Comparator进行排序:
TreeSet treeSet = new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
// 自定义按照字符串的顺序升序排序
return o1.toString().compareTo(o2.toString());
}
});
treeSet.add("zhangsan");
treeSet.add("lisi");
treeSet.add("arbor");
treeSet.add("wangwu");
① 调用的是带有比较器的构造方法,可以看到在内部,new了一个TreeMap
的对象,并把比较器传给了TreeMap
。
② TreeMap中有一个comparator属性,会将默认的比较器替换为传入的比较器。
③ 调用add方法,可以看到,底层调用的是put方法,实际上也是TreeMap的put方法
④ 而在put方法中:
重点: 如果比较器得到的结果为0,是不添加该元素,而是替换value的值,因为TreeSet没有value,所以直接不添加元素。
.
比如,按照两个字符串的长度排序,如果字符串长度一样,就添加不进去。
public V put(K key, V value) {
Entry<K,V> t = root;
// 这里省略一些代码,主要作用是:
// 第一次添加元素时,也会触发比较器,自己和自己比较
// 主要是为了预防传入null值
int cmp;
Entry<K,V> parent;
// cpr就是自己传入的比较器
// 然后根据比较规则添加元素
Comparator<? super K> cpr = comparator;
if (cpr != null) {
do {
parent = t;
cmp = cpr.compare(key, t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
// 如果比较的时候相等,cmp==0的情况下:
// 这个数据就不加入(因为是set集合,单列的)
// 如果不是从TreeSet过来的,会替换value值
return t.setValue(value);
} while (t != null);
}
// 如果没有传比较器,就走这条线,按照默认规则排序
else {
if (key == null)
throw new NullPointerException();
@SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key;
do {
parent = t;
cmp = k.compareTo(t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
} while (t != null);
}
Entry<K,V> e = new Entry<>(key, value, parent);
if (cmp < 0)
parent.left = e;
else
parent.right = e;
fixAfterInsertion(e);
size++;
modCount++;
return null;
}
TreeMap
TreeMap的使用方式和Map接口使用方式差不多。
Java中的TreeMap
用于存储与HashMap
类非常相似的键值对。区别在于TreeMap
提供了一种以排序顺序存储键/值对的有效方法。它是基于红黑树的NavigableMap
实现。
key不能为null,但value可以为null。
TreeMap的元素支持2种排序方式:自然排序或者根据提供的Comparator进行排序。
TreeMap treeMap = new TreeMap(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return o1.toString().length() - o2.toString().length();
}
});
treeMap.put("zhangsan", "张三");
treeMap.put("lisi", "李四");
treeMap.put("arbor", "乔木先生");
treeMap.put("wangwu", "王五");
treeMap.put("xiaoming", "小明");
执行过程和TreeSet
一样,不同的是如果调用compare方法,得到的结果为0,则替换value值。
而TreeSet
因为没有value,所以不会添加元素。