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<K,V> extends AbstractMap<K,V> implements Map<K,V>,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.

<span style="background-color: rgb(255, 204, 102);">Set<Map.Entry<K,V>>	entrySet()</span>
Returns a Set view of the mappings contained in this map.

<span style="background-color: rgb(255, 204, 102);"> V	get(Object key)</span>
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.
<span style="background-color: rgb(255, 204, 102);">
Set<K>	keySet()</span>
Returns a Set view of the keys contained in this map.

<span style="background-color: rgb(255, 204, 102);">V	put(K key, V value)</span>
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<V>	values()
Returns a Collection view of the values contained in this map.




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值