底层原理Hashmap源码解析实例

Map.java

 1 package com.collection;
 2 
 3 public interface Map<K, V> {
 4     public V put(K k, V v);
 5 
 6     public V get(K k);
 7 
 8     public int size();
 9 
10     interface Entry<K, V> {
11         public K getKey();
12 
13         public V getValue();
14     }
15 }

 

HashMap.java

 1 package com.collection;
 2 
 3 public class HashMap<K, V> implements Map<K, V> {
 4     private Entry<K, V>[] table = null;
 5     private int size = 0;
 6     private static int defaultLength = 16;
 7 
 8     public HashMap() {
 9         table = new Entry[defaultLength];
10     }
11 
12     @Override
13     public V put(K k, V v) {
14         int index = hash(k);
15         Entry<K, V> entry = table[index];
16         if (entry == null) {
17             table[index] = new Entry<K, V>(k, v, null, index);
18             size++;
19         } else {
20             table[index] = new Entry<K, V>(k, v, entry, index);
21         }
22         return table[index].getValue();
23     }
24 
25     private int hash(K k) {
26         int index = k.hashCode() & (defaultLength - 1);
27         return Math.abs(index);
28     }
29 
30     @Override
31     public V get(K k) {
32         if (size == 0) {
33             return null;
34         }
35         int index = hash(k);
36         Entry<K, V> entry = getEntry(k, index);
37 
38         return entry == null ? null : entry.getValue();
39     }
40 
41     private Entry<K, V> getEntry(K k, int index) {
42         for (Entry<K, V> entry = table[index]; entry != null; entry = entry.next) {
43             if (entry.hash == index && (k == entry.getKey() || k.equals(entry.getKey()))) {
44                 return entry;
45             }
46         }
47         return null;
48     }
49 
50     @Override
51     public int size() {
52         return 0;
53     }
54 
55     class Entry<K, V> implements Map.Entry<K, V> {
56         K k;
57         V v;
58         Entry<K, V> next;
59         int hash;
60 
61         public Entry(K k, V v, Entry<K, V> next, int hash) {
62             this.k = k;
63             this.v = v;
64             this.next = next;
65             this.hash = hash;
66         }
67 
68         @Override
69         public K getKey() {
70             return k;
71         }
72 
73         @Override
74         public V getValue() {
75             return v;
76         }
77     }
78 }

TestHashMap.java

 1 package com.collection;
 2 
 3 public class TestHashMap {
 4     public static void main(String[] args) {
 5         com.collection.HashMap map = new com.collection.HashMap<>();
 6 
 7 
 8         for (int i = 0; i < 6666; i++) {
 9             map.put("Monkey" + i, "計算方法的事實");
10             System.out.println(map.get("Monkey"));
11         }
12 
13         System.out.println(map);
14 
15     }
16 }

 

转载于:https://www.cnblogs.com/cxxjohnson/p/10736700.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HashMapJava中常用的数据结构,用于存储键值对,并支持O(1)时间复杂度的插入、查询、删除等操作。 HashMap源码解析如下: 1. HashMap是一个实现了Map接口的类,内部使用数组和链表实现。 2. HashMap中的键值对是以Entry对象的形式存储的,每个Entry对象包含一个键、一个值和指向下一个Entry对象的引用。 3. HashMap内部维护了一个默认容量为16的数组table,负载因子为0.75,默认扩容因子为2。当HashMap中的元素数量超过容量与负载因子的乘积时,即会触发扩容操作。 4. HashMap使用哈希函数将键映射到对应的数组下标上,实现快速查询。 5. 如果哈希函数产生了哈希冲突,即多个键映射到同一个数组下标上,HashMap会使用链表将这些键值对串起来,以便查询时遍历链表查找。 6. 在插入新的键值对时,HashMap会根据哈希函数计算出对应的数组下标,并将新的键值对插入到该位置的链表中。如果该位置的链表长度超过阈值(默认为8),则将这个链表转化为红黑树,以提高查询效率。 7. 在查询、删除键值对时,HashMap根据哈希函数计算出对应的数组下标,并遍历该位置的链表或红黑树,查找对应的键值对。如果链表或红黑树中没有对应的键值对,则返回null。 总之,HashMap是一个高效的数据结构,能够快速地插入、查询、删除键值对。不过,对于高度散列的数据集,也可能导致哈希冲突的增加,进而导致查询效率下降。因此,在使用HashMap时,需要合理地设置容量和负载因子,以及注意键的哈希函数的设计。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值