容器的扩展-2

来源:互联网 发布:ed2k for mac下载工具 编辑:程序博客网 时间:2024/06/08 13:27

HashTable和Properties

Hashtable与HashMap区别:

  1. 主要:Hashtable线程安全,同步,效率低下
            HashMap线程不安全,非同步,效率相对高

  2. 父类:Hashtable extends Dictionary;
            HashMap extends AbstractMap

  3. null:Hashtable键与值不能为null
           HashMap键最多一个null,值可以多个null.

对于Properties,是Hashtable的子类,但是它键与值只能为字符串

public class TestPeoperties {    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");        //获取值,如果没有值的话就输出后面的值"null"        System.out.println(pro.getProperty("url1", "null"));    }   }
import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;/** * Properties 资源配置文件的读写 * 将其写入文件中 *  * .properties * 1、Store(Writer w,String comments) * 2、Store(OutputStreaam os,String comments) *  * .XML * 1、StoreToXML(Writer w,String comments) * 2、StoreToXML(OutputStreaam os,String comments) * @author Administrator * */public class TestPeoperties_1 {    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");        //存储到D:/安装包/aa中,使用绝对路径,也就是带盘符的-->  :        pro.store(new FileOutputStream(new File("D:/安装包/aa/db.properties")), "db配置");        pro.storeToXML(new FileOutputStream(new File("D:/安装包/aa/db.xml")), "db配置");        //使用相对路径,也就是去掉盘符,默认路径为当前工程        pro.store(new FileOutputStream(new File("src/cn/feng/queue/db.properties")), "db配置");        pro.storeToXML(new FileOutputStream(new File("db.xml")), "db配置");    }}
import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.util.Properties;/** * 使用Properties读取配置文件,使用一下方法 * load(InputStream inStream)  * load(Reader reader)  * loadFromXML(InputStream inStream) * @author Administrator * */public class TestProperties_3 {    public static void main(String[] args) throws FileNotFoundException, IOException {        Properties pro = new Properties();        //读取,按照绝对路径        pro.load(new FileReader(new File("D:/安装包/aa/db.properties")));        System.out.println(pro.getProperty("user", "null"));        //读取,按照相对路径        pro.load(new FileReader(new File("src/cn/feng/queue/db.properties")));        System.out.println(pro.getProperty("url", "null"));    }}
import java.io.IOException;import java.util.Properties;/** * 使用类相对路径读取配置文件 * @author Administrator *  */public class TestProperties_2 {    public static void main(String[] args) throws IOException {        Properties pro = new Properties();        //类相对路径,"/"表示根目录,当前为bin目录        pro.load(TestProperties_2.class.getResourceAsStream("/cn/feng/queue/db.properties"));        System.out.println(pro.getProperty("user","null")) ;        //和上面的不同,这里采用空表示根目录,当前为bin目录        //下面的代码分别对应↓当前线程,main线程里↓↓上下文的类加载器↓↓默认从根目录下取资源↓         pro.load(Thread.currentThread().getContextClassLoader().        getResourceAsStream("cn/feng/queue/db.properties"));        System.out.println(pro.getProperty("url","null")) ;    }}

WeakHashMap、IdentityHashMap和EnumMap

java语言提供了4种引用类型:强引用(StrongReference)、软引用(SoftReference)、弱引用(WeakReference)和幽灵引用(PhantomReference),与引用密切相关的,还有一个引用队列ReferenceQueue。

  • 强引用:垃圾回收器绝不会回收它。当内存空间不足,Java虚拟机宁愿抛出OutOfMemoryError错误,使程序异常终止,也不会靠随意回收具有强引用的对象来解决内存不足问题。

  • 弱引用:运行时立即回收一旦发现了只具有弱引用的对象,不管当前内存空间足够与否,都会回收它的内存。

  • 软引用:内存空间足够,垃圾回收器就不会回收它,如果内存空间不足了,就会回收这些对象的内存

  • 虚引用:虚引用主要用来跟踪对象被垃圾回收器回收的活动。虚引用与软引用和弱引用的一个区别在于:虚引用必须和引用队列 (ReferenceQueue)联合使用。当垃圾回收器准备回收一个对象时,如果发现它还有虚引用,就会在回收对象的内存之前,把这个虚引用加入到与之关联的引用队列中。

import java.lang.ref.WeakReference;public class RefDemo {    public static void main(String[] args) {        //存放在字符串常量池中,是共享的,不能被回收        String str ="lf is the god";        String str1 = new String("lf is the god");        WeakReference<String> wr = new WeakReference<String>(str);        WeakReference<String> wr1 = new WeakReference<String>(str1);        System.out.println("gc回收前:"+wr.get());        System.out.println("gc回收前:"+wr1.get());        str = null;        str1 = null;        System.gc();        System.runFinalization();        System.out.println("gc回收前:"+wr.get());        System.out.println("gc回收前:"+wr1.get());    }}
输出结果:gc回收前:lf is the god        gc回收前:lf is the god        gc回收前:lf is the god        gc回收前:null
import java.util.WeakHashMap;/** * WeakHashMap 要求键为弱引用,当数据运行完之后就可以进行回收 * @author Administrator * */public class TestWeakHashMap {    public static void main(String[] args) {        WeakHashMap<String, String> map = new WeakHashMap<>();        //测试数据        //常量池对象,不会回收        map.put("abc", "a");        map.put("d", "test");        //gc运行 已被回收        map.put(new String("bjsxt"), "c");        map.put(new String("dsf"), "d");        System.out.println(map.size());  //输出4        System.gc();        System.runFinalization();        System.out.println(map.size());  //输出2    }}
import java.util.IdentityHashMap;/** * IdentityHashMap 键地址去重,并不比较hashcode和equals * @author Administrator * */public class TestIdentityHashMap {     public static void main(String[] args) {        IdentityHashMap<String ,String> map =new IdentityHashMap<String,String>();        //常量池中的"a"        map.put("a", "a1");        map.put("a", "a2");        System.out.println(map.size());  //输出结果为1        map.put(new String("a"), "a3");        map.put(new String("a"), "a4");        System.out.println(map.size());  //输出结果为3     }}
import java.util.EnumMap;/** * EnumMap 要求键必须为枚举,而枚举指的是常量的集合 * @author Administrator * */public class TestEnumMap {    public static void main(String[] args) {        EnumMap<Season,String> map =new EnumMap<Season,String>(Season.class);        //存放值        map.put(Season.SPRING, "春困");        map.put(Season.SUMMER, "夏无力");        map.put(Season.AUTUMN, "秋乏");        map.put(Season.WINTER, "冬眠");        System.out.println(map.size());    }}//季节enum Season{  SPRING,SUMMER,AUTUMN,WINTER}