打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
HashMap底层实现原理,以及和Hashtable的比较

boolean containsValue(Object value)Returns true if this map maps one or more keys to the specified value.

首先,我们要知道HashMap底层实现是数组(Entry类型)加上链表的数据结构---拉链法实现哈希表

Entry 实现了Map.Entry 接口,即实现getKey(), getValue(), setValue(V value), equals(Object o), hashCode()这些函数

                        


    1.通过hash()函数,计算key对应的哈希值,找到数组中存储位置,在冲突(不同的关键字,对应相同的哈希值,在)较少的情况下,他的查询效率是很高的,如果发生冲突就在数组元素所在链表的表头插入该值,所以在查找HashMap时候有两个关键函数,一个是hash()函数找到key在数组中的位置,一个是equals()函数,在链表中找到key,返回value,通常返回某关键字时候用的判断方法:(key==null ? k==null :key.equlas(k)),可见关键字可以为null

   2.Hashtable 的实例有两个参数影响其性能:初始容量 和 加载因子。容量是哈希表中桶的数量,初始容量 就是哈希表创建时的容量。注意,哈希表的状态为 open:在发生“哈希冲突”的情况下,单个桶会存储多个条目,这些条目必须按顺序搜索。加载因子是对哈希表在其容量自动增加之前可以达到多满的一个尺度。初始容量和加载因子这两个参数只是对该实现的提示。关于何时以及是否调用 rehash 方法的具体细节则依赖于该实现。通常,默认加载因子是 0.75(当数据元素个数达到数组总容量的75%时候,要对数组进行动态扩展), 这是在时间和空间成本上寻求一种折衷。加载因子过高虽然减少了空间开销,但同时也增加了查找某个条目的时间(在大多数 Hashtable 操作中,包括 get 和 put 操作,都反映了这一点)。

  3.public class HashMap extends AbstractMap implements Map,HashMap继承于AbstractMap父类,并且是非线程同步的


Hashtable和HashMap采用的hash/rehash算法都大概一样,所以性能不会有很大的差异。

关于HashMap和Hashtable的区别:

1.由于历史原因,Hashtable是基于Dictionary类,HashMap是1.2之后实现Map接口的实现(从其命名的不规范就可以看出年代之久。正常应该是HashTable,如果修改的,大量程序需要修改

2.最重要的不同是HashTable是同步的(每个方法前面都加上了synchronized关键字),而HashMap不是线程同步的,可以利用Collections类的synchronizedMap()方法创建一个安全的Map对象,并把它作为一个封装对象返回,利用这个对象可以让你同步访问潜在的HashMap.

Map m = Collections.synchronizedMap(new HashMap(...));

3.HashMap中key,value允许为空,即HashMap中只有一条记录可以是一个空的key,但任意数量的条目可以是空的value。或者如果发现了搜索键,但它是一个空的值,那么get(object key)将返回null。如果有必要,用containKey()方法来区别这两种情况。Hashtable的key,value不允许为空



下面列出来一些HashMap常用的方法:

boolean containsKey(Object key)Returns true if this map contains a mapping for the specified key.boolean containsValue(Object value)Returns true if this map maps one or more keys to the specified value.Set<>> entrySet()Returns a Set view of the mappings contained in this map. V get(Object key)Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.boolean isEmpty()Returns true if this map contains no key-value mappings.Set keySet()Returns a Set view of the keys contained in this map.V put(K key, V value)Associates the specified value with the specified key in this map.V remove(Object key)Removes the mapping for the specified key from this map if present.boolean remove(Object key, Object value)Removes the entry for the specified key only if it is currently mapped to the specified value.V replace(K key, V value)Replaces the entry for the specified key only if it is currently mapped to some value.int size()Returns the number of key-value mappings in this map.Collection values()Returns a Collection view of the values contained in this map.




本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
HashMap和HashTable,HashMap中key和value的原理
Map接口及其重要实现类的用法
浅析Java中Map与HashMap,Hashtable,HashSet的区别
【JAVA知识】HashTable和HashMap的区别,并简述Hashmap的实现原理
Map接口
HashMap和Hashtable的区别
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服