zoukankan      html  css  js  c++  java
  • Java基础之:集合——Map——HashTable

    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("操作成功~~");
        }
    }
    

      

     

     

  • 相关阅读:
    SQL server 统计数据库表数量和列出所有表名称
    mybatis 模糊查询 like的三种方式
    jquery 实现按回车键登录功能的写法
    js 各种事件 如:点击事件、失去焦点、键盘事件等
    ssm框架中从controller传值给jsp的方式
    [GDOI2019]小说
    洛谷5113
    2020.9.26模拟总结
    [IOI2015]分组
    9.19 总结
  • 原文地址:https://www.cnblogs.com/SongHai/p/14195399.html
Copyright © 2011-2022 走看看