分享

Java基础之:集合——Map——HashTable

 精品唯居 2021-06-06

 

 

Java基础之:集合——Map——HashTable

HashTable简单介绍

  1. This class implements a hash table[该类实现hashtable]

  2. which maps keys to values [元素是键值对]

  3. Any non-null object can be used as a key or as a value [hashtable的键和值都不能为null]

  4. 所以是从上面看,hashTable 基本上和hashMap一样的.

  5. hashTable 是线程安全的,hashMap 是线程不安全的.

简单使用案例

package class_Map;
import java.util.Hashtable;
public class ClassTest03_HashTable {
​
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void main(String[] args) {
        Hashtable table = new Hashtable();// ok
        table.put("john", 100); // ok
        // table.put(null, 100); //错误 , 键不能为 null, 抛出异常
        // table.put("john", null);//错误 , 值不能为 null, 抛出异常
        table.put("lucy", 100);// ok
        table.put("lic", 100);// ok
        System.out.println(table);
    }
}

HashTable与HashMap对比

 

 

Properties

Properties是继承了HashTable的子类,主要用于文件io流当中。

使用Properties读取文件

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
​
public class Properties01 {
​
    public static void main(String[] args) {
​
        // 创建一个 Properties 对象, 按照 key-value 来进行管理的
        Properties prop = new Properties();
        // 对文件操作
        try {
            // 功能,将 a.properites 文件读取
            // 读取属性文件a.properties
            // 文件编程内容,你们 1 day 讲解
            // 创建一个 InputStream, 就可以读取 a.properties
            // in 的编译类型 InputStream, 运行类型 BufferedInputStream
            InputStream in = new BufferedInputStream(new FileInputStream("src\\my.properties"));
            prop.load(in); /// 加载属性列表
​
            // 泛型<String>
            Iterator<String> it = prop.stringPropertyNames().iterator();
            // 迭代器读取
            while (it.hasNext()) {
                String key = it.next();
                /*
                 * 
                 * user=hsp pwd=123456 ip=192.168.11.11
                 */
                System.out.println(key + ":" + prop.getProperty(key));
            }
            // 关闭流
            in.close();
​
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.getMessage());
        }
​
    }
}

 

使用Properties写文件

import java.io.FileOutputStream;
import java.util.Properties;
​
public class Properties02 {
​
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // 创建一个 Properties 对象, 按照 key-value 来进行管理的
        Properties prop = new Properties();
        try {
​
            /// 保存属性到a.properties文件
            //  创建了一个 FileOutputStream 对象,用于向文件中 a.properties 写入内容
            FileOutputStream oFile = new FileOutputStream("src\\my.properties", true);// true表示追加打开
            prop.setProperty("db", "order"); //增加
            //
            //prop.store(oFile, "The New properties file");
            prop.store(oFile, ""); //如果是空串就没有多余信息了..
            oFile.close();
​
        } catch (Exception e) {
            // TODO: handle exception
            System.err.println(e.getMessage());
        }
        System.out.println("操作成功~~");
    }
}

  

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约