HashTable
一、HashTable的基本介绍

- 存放的元素是键值对:即
K-V
hashTable
的键和值都不能为null,否则会抛出NullPointerException
hashTable
使用方法基本上和HashMap一样
hashTable
是线程安全的,hashMap
是线程不安全
- 简单看下底层结构

- 在底层是一个数组
- Hashtable$Entry[] 初始化大小为 11
- 临界值:
threshold 8 = 11 * 0.75
- 扩容:按照自己的扩容机制来扩容即可
- 执行 方法
addEntry(hash, key, value, index);
,添加K-V封装到Entry里面去
- 按照
int newCapacity = (oldCapacity << 1) + 1;
进行扩容
二、HashTable和HashMap对比
|
版本 |
线程安全(同步) |
效率 |
允许null键null值 |
HashMap |
1.2 |
不安全 |
高 |
可以 |
HashTable |
1.0 |
安全 |
较低 |
不可以 |
三、Properties
- Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存数据。
- 他的使用特点和Hashtable类似
- Properties还可以用于从 xxx.properties文件中,加载数据到Properties类对象,并进行读取和修改
- 说明:工作后xxx.properties文件通常作为配置文件,这个知识点在IO流举例
package com.hspedu.map_;
import java.util.Properties;
public class Properties_ {
public static void main(String[] args) {
Properties properties = new Properties();
properties.put("john",100);
properties.put("lucy",100);
properties.put("lic",100);
properties.put("lic",88);
System.out.println(properties);
System.out.println(properties.get("lic"));
properties.remove("lic");
System.out.println(properties);
properties.put("john","约翰");
System.out.println(properties);
System.out.println(properties.get("john"));
System.out.println(properties.getProperty("john"));
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期