java map hashtable_[Java教程]Java基础之:集合——Map——HashTable

[Java教程]Java基础之:集合——Map——HashTable

0 2020-12-27 09:00:20

bc91bb04e6e9c61e24c974e4440db8f2.gif

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

HashTable简单介绍This class implements a hash table[该类实现hashtable]

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

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

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

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对比

bc91bb04e6e9c61e24c974e4440db8f2.gif

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); /// 加载属性列表​ // 泛型 Iterator 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("操作成功~~"); }}

本文网址:http://www.shaoqun.com/a/504123.html

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:admin@shaoqun.com。

Java

0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值