Java Hashtable和Properties

Hashtable

  Hashtable方法都带有synchronized:线程安全的。
    线程安全有其它的方案,这个Hashtable对线程的处理导致效率较低,使用较少了。

  Hashtable和HashMap一样,底层都是哈希表数据结构。
    Hashtable的初始化容量是11,默认加载因子是:0.75f
    Hashtable的扩容是:原容量 * 2 + 1


Properties
  Properties是一个Map集合,继承Hashtable,Properties的key和value都是String类型。
  Properties被称为属性类对象。
  Properties是线程安全的。

public class PropertiesTest01 {
    public static void main(String[] args) {

        // 创建一个Properties对象
        Properties pro = new Properties();

        // 需要掌握Properties的两个方法,一个存,一个取。
        pro.setProperty("url", "jdbc:mysql://localhost:3306/bjpowernode");
        pro.setProperty("driver","com.mysql.jdbc.Driver");
        pro.setProperty("username", "root");
        pro.setProperty("password", "123");

        // 通过key获取value
        String url = pro.getProperty("url");
        String driver = pro.getProperty("driver");
        String username = pro.getProperty("username");
        String password = pro.getProperty("password");

        System.out.println(url);
        System.out.println(driver);
        System.out.println(username);
        System.out.println(password);

    }
}

 

posted @ 2020-09-17 22:44  一叶扁舟,乘风破浪  阅读(236)  评论(0编辑  收藏  举报