1.HashTable
1.基本介绍
1.存放的元素是键值对:K-V
2.hashtable的键值不能为null,否则会抛出 空指针异常
3.hashtable是线程安全的(synchronized),hashMap是线程不安全的。
public class test_Hashtable {
public static void main(String[] args) {
HashMap<String,String> map = new HashMap<>();
map.put(null,"a");
map.put("b",null);
map.put(null,null);
System.out.println(map); //{null=null, b=null}
Hashtable<String,String> table = new Hashtable<>();
//空指针异常
//table.put(null,"a");
table.put("a","b");
System.out.println(table);
}
}
2.Properties
1.基本介绍
1.Properties类继承自HashTable类并实现了Map接口,也是使用键值对的形式来保存数据。
使用Map的put、get方法也是可以使用的。
2.该集合没有泛型。键值都是字符串。
3.Properties还可以用于从 xxx.properties 文件中,加载数据到Properties类对象,并进行读取和修改。
4.它是一个可以持久化的属性集。键值可以存储到集合中,也可以存储到持久化的设备(硬盘、U盘、光盘)上。键值的来源也可以是持久化的设备。
public class Test_01_Properties {
public static void main(String[] args) throws IOException{
show01();
show02();
show03();
}
/** Properties集合存储的数据,遍历取出Properties集合的数据
* Properties集合是一个双列集合,key和value默认都是字符串
* Properties有一些特殊操作字符串的方法
* Object setProperty(String key,String value) 调用Hashtable方法的put
* String getProperties(String key)通过key找到value值,此方法相当于Map集合中的get(key)方法
* Set<String> StringPropertyNames()返回此属性中的键值,其中该键以及对应的值都是字符串
*
* */
private static void show01() {
//创建Properties对象
Properties p = new Properties();
//使用setProperty往集合中添加数据
//如果已经有键,那么这个方法就是修改
p.setProperty("找","166");
p.setProperty("到","167");
p.setProperty("了","169");
//Set<String> StringPropertyNames()存储到一个集合当中
Set<String> set = p.stringPropertyNames();
//遍历集合
for(String key : set) {
//使用setProperties获取value
String value = p.getProperty(key);
System.out.println(key + " value " + value);
}
System.out.println();
}
/**
* void store(OutputStream out, String comments)将此属性列表(键和元素对)写入此 Properties表中
* void store(Writer writer, String comments)
* 数据:
* OutputStream out:字节输出流,不能写入中文
* Writer writer 字符输出流,可以写中文
* String comments注释,用老保存文件是做什么的
* 一般不使用中文会乱码
* 一般使用空字符
* 使用步骤:
* 1、创建Properties集合对象,添加数据
* 2、创建字节输出流/字符输出流对象,构造方法中绑定输出的目的地
* 3、使用store方法,把集合中的临时数据,持久化写入到硬盘中
* 4、释放资源
* */
private static void show02() throws IOException {
//1、创建Properties对象
Properties p = new Properties();
//使用setProperty往集合中添加数据
p.setProperty("找","166");
p.setProperty("到","167");
p.setProperty("了","169");
// 2、创建字节输出流/字符输出流对象,构造方法中绑定输出的目的地
FileWriter fw = new FileWriter("src/note");
//3、使用store方法,把集合中的临时数据,持久化写入到硬盘中
p.store(fw,"save data");
//释放
fw.close();
}
/** 可以使用Properties集合中的方法,把硬盘中保存的文件(键值对),读取到集合中去
* void load(InputStream inStream)字节输入流,不能读取含有中文的键值对
* void load(Reader reader)字符输入流,能读取含有中文的键值对
*
* 使用步骤:
* 1、创建Properties集合对象
* 2、使用Properties集合对象中的方法load读取保存键值对的文件
* 3、遍历Properties集合
* 注意:
* 1、存储键值对的文件中,键与值默认的连接符可以使用=,空格(其他符号)
* 2、存储键值对的文件中,可以使用#进行注释,被注释的键值对不会再被读取
* 3、存储键值对的文件中,键与值默认都是字符串,不用再加上引号
*/
private static void show03() throws IOException{
//1、创建Properties集合对象
Properties prop = new Properties();
//2、使用Properties集合对象中的方法load读取保存键值对的文件
prop.load(new FileReader("src/note"));
//3、遍历Properties集合
Set<String> set = prop.stringPropertyNames();
for (String key : set) {
String v = prop.getProperty(key);
System.out.println(key + " = " +v);
}
}
}