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

      

     

     

  • 相关阅读:
    Android 中的code sign
    iOS 中的Certificate,Provisioning Profile 的一些注意 (不断完善中)
    xcode 和 android studio中在Mac系统下的自动对齐快捷键
    iOS block 声明时和定义时的不同格式
    iOS 和 Android 中的后台运行问题
    Android 阅读Tasks and Back Stack文章后的重点摘抄
    Android 中PendingIntent---附带解决AlarmManager重复加入问题
    Android 中获得notification的发出时间
    iOS 关于Layer的疑问
    iOS的 context 和Android 中的 canvas
  • 原文地址:https://www.cnblogs.com/SongHai/p/14195399.html
Copyright © 2020-2023  润新知