当前位置: 首页 > news >正文

Java(HashMap和HashTable和Properties)

HashMap:

HashMap底层原理分析:

源码:

1.执行构造器 new HashMap()
初始化加载因子 loadfactor = 0.75
HashMap$Node[] table = null

2. 执行 put 调用 hash 方法,计算 key 的 hash 值 (h = key.hashCode()) ^ (h >>> 16)
public V put(K key, V value) {//K = "java" value = 10
return putVal(hash(key), key, value, false, true);
}

3. 执行 putVal
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {            
Node<K,V>[] tab; Node<K,V> p; int n, i;//辅助变量
//如果底层的 table 数组为 null, 或者 length =0 , 就扩容到 16
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;

//取出 hash 值对应的 table 的索引位置的 Node, 如果为 null, 就直接把加入的 k-v
//, 创建成一个 Node ,加入该位置即可
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;//辅助变量
// 如果 table 的索引位置的 key 的 hash 相同和新的 key 的 hash 值相同,
// 并 满足(table 现有的结点的 key 和准备添加的 key 是同一个对象 || equals 返回真)
// 就认为不能加入新的 k-v
if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)//如果当前的 table 的已有的 Node 是红黑树,就按照红黑树的方式处

e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
//如果找到的结点,后面是链表,就循环比较
for (int binCount = 0; ; ++binCount) {//死循环
if ((e = p.next) == null) {//如果整个链表,没有和他相同,就加到该链表的最后
p.next = newNode(hash, key, value, null);
//加入后,判断当前链表的个数,是否已经到 8 个,到 8 个,后
//就调用 treeifyBin 方法进行红黑树的转换
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}

              if (e.hash == hash && //如果在循环比较过程中,发现有相同,就 break,就只是替换 value
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value; //替换,key 对应 value
afterNodeAccess(e);
return oldValue;
}
}
++modCount;//每增加一个 Node ,就 size++
if (++size > threshold[12-24-48])//如 size > 临界值,就扩容
resize();
afterNodeInsertion(evict);
return null;
}
5. 关于树化(转成红黑树)
//如果 table 为 null ,或者大小还没有到 64,暂时不树化,而是进行扩容.
//否则才会真正的树化 -> 剪枝
final void treeifyBin(Node<K,V>[] tab, int hash) {
int n, index; Node<K,V> e;

if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
resize();
}
}
}

 HashTable 的基本介绍:

Hashtable 和 HashMap 对比:

Properties:

Properties的方法:
put():添加/修改

remove():删除

开发中如何选择集合实现类:

TreeSet:

//1. 当我们使用无参构造器,创建 TreeSet 时,仍然是无序的
//2. 老师希望添加的元素,按照字符串大小来排序
//3. 使用 TreeSet 提供的一个构造器,可以传入一个比较器(匿名内部类)
// 并指定排序规则

源码:

1. 构造器把传入的比较器对象,赋给了 TreeSet 的底层的 TreeMap 的属性 this.comparator
public TreeMap(Comparator<? super K> comparator) {
this.comparator = comparator;
}
2. 在 调用 treeSet.add("tom"), 在底层会执行到
if (cpr != null) {//cpr 就是我们的匿名内部类(对象)

        do {
parent = t;
//动态绑定到我们的匿名内部类(对象)compare
cmp = cpr.compare(key, t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else //如果相等,即返回 0,这个 Key 就没有加入
return t.setValue(value);
} while (t != null);
}

TreeMap:
1. 构造器. 把传入的实现了 Comparator 接口的匿名内部类(对象),传给给 TreeMap 的 comparator
public TreeMap(Comparator<? super K> comparator) {
this.comparator = comparator;
}
2. 调用 put 方法
2.1 第一次添加, 把 k-v 封装到 Entry 对象,放入 root
Entry<K,V> t = root;
if (t == null) {
compare(key, key); // type (and possibly null) check
root = new Entry<>(key, value, null);
size = 1;
modCount++;
return null;
}

2.2 以后添加
Comparator<? super K> cpr = comparator;
if (cpr != null) {
do { //遍历所有的 key , 给当前 key 找到适当位置
parent = t;
cmp = cpr.compare(key, t.key);//动态绑定到我们的匿名内部类的 compare
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else //如果遍历过程中,发现准备添加 Key 和当前已有的 Key 相等,就不添加

eturn t.setValue(value);
} while (t != null);
}
}
}

http://www.dtcms.com/a/312850.html

相关文章:

  • kafka 是一个怎样的系统?是消息队列(MQ)还是一个分布式流处理平台?
  • 哔哩哔哩招游戏内容产品运营
  • Ubuntu22.4部署大模型前置安装
  • 零确认双花攻击
  • 智变时代:AI 如何重构工作边界与行业生态?
  • 【软考中级网络工程师】知识点之 IS-IS 协议
  • 百度招黑产溯源安全工程师
  • Spring AOP_2
  • CPA全国青少年编程能力等级测评试卷及答案 Python编程(一级)
  • 【代码详解】Triplane Meets Gaussian Splatting中triplane部分解析
  • 【代码随想录|454.四数相加、383.赎金信、15.三数之和、18.四数之和】
  • 模拟-6.N字形变换-力扣(LeetCode)
  • GPIO交换矩阵和IO_MUX
  • Python Seaborn【数据可视化库】 全面讲解
  • node.js常用函数
  • web前端React和Vue框架与库安全实践
  • Elastic 9.1/8.19:默认启用 BBQ,ES|QL 支持跨集群搜索(CCS)正式版,JOINS 正式版,集成 Azure AI Foundry
  • Python爬虫实战:研究awesome-python工具,构建技术资源采集系统
  • 【C语言】结构体详解
  • 第15届蓝桥杯Scratch图形化国赛初/中级组2024年9月7日真题
  • 使用DrissionPage实现xhs笔记自动翻页并爬取笔记视频、图片
  • 禁闭求生2 免安 中文 离线运行版
  • 初识prometheus
  • 控制建模matlab练习04:二阶系统的时域响应
  • Supergateway教程
  • 前端技术制作简单网页游戏
  • 力扣457:环形数组是否存在循环
  • 【Excel】利用函数和Power Query进行数据分析
  • Java企业级应用性能优化实战
  • 控制建模matlab练习09:超前补偿器