Hashtable与HashMap区别:
1)主要:Hashtable线程安全,同步,效率低下
HashMap线程不安全,非同步,效率相对高
2)父类:Hashtable extends Dictionary;HashMap extends AbstractMap
3)null:Hashtable键与值不能为null
HashMap键最多一个null,值可以多个null.
package collection.others.HashMap_HashTable_Properties;
import java.util.Properties;
/**Properties
* 作用:读取资源配置文件
* 1、key 与value 只能为字符串
* 2、存储与读取
* setProperty(String key,String value);
* getProperty(String key);
* getProperty(String key,String defaultValue);
*/
public class ProperitiesTest {
public static void main(String[] args) {
Properties pro =new Properties();
pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
pro.setProperty("user", "scott");
pro.setProperty("pwd", "tiger");
String url =pro.getProperty("url","test");
System.out.println(url);
}
}
package collection.others.HashMap_HashTable_Properties;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* 使用Properties输出到文件
* 资源配置文件:
* 1、.properties
* store(OutputStream out, String comments)
store(Writer writer, String comments)
2、.xml
storeToXML(OutputStream os, String comment) :UTF-8字符集
storeToXML(OutputStream os, String comment, String encoding)
*/
public class PropertiesStore {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pro =new Properties();
pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
pro.setProperty("user", "scott");
pro.setProperty("pwd", "tiger");
pro.store(new FileOutputStream(new File("src/db.properties")), "db配置");
pro.storeToXML(new FileOutputStream(new File("src/db.properties")), "db配置");
}
}
package collection.others.HashMap_HashTable_Properties;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
/**
* 使用Properties读取配置文件
* 资源配置文件:
* 使用相对与绝对路径读取
* load(InputStream inStream)
load(Reader reader)
loadFromXML(InputStream inStream)
*/
public class Propertiesload {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pro=new Properties();
pro.load(new FileReader("src/db.properties"));
System.out.println(pro.getProperty("user", "bjsxt"));
}
}
package collection.others.HashMap_HashTable_Properties;
import java.io.IOException;
import java.util.Properties;
/**
* 使用类相对路径读取配置文件
* bin
*/
public class PropertiesClassPath {
public static void main(String[] args) throws IOException {
Properties pro =new Properties();
pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/bjsxt/others/pro/db.properties"));
System.out.println(pro.getProperty("user", "bjsxt"));
}
}