HashMap和HashTable的区别及常用方法

HashMap和HashTable的区别以及使用

HashMap

  • 底层由数组和链表实现,可以存储null键和null值,实现不同步所以线程不安全,映射不时有序的
  • 初始容量为16,扩容方法:newsize = oldsize*2,所以容量一定为2的n次幂
  • 扩容针对整个Map,每次扩容时,原来数组中的元素依次重新计算存放位置,并重新插入
  • 插入元素后才判断该不该扩容,有可能无效扩容(插入后如果扩容,所以如果没有再次插入,就会产生无效扩容)
  • 当Map中元素总数超过Entry数组的75%,触发扩容操作,为了减少链表长度,元素分配更均匀
  • 计算index方法:index = hash & (tab.length – 1)
HashMap的构造函数
// 默认构造函数。
HashMap()

// 指定“容量大小”的构造函数
HashMap(int capacity)

// 指定“容量大小”和“加载因子”的构造函数
HashMap(int capacity, float loadFactor)

// 包含“子Map”的构造函数
HashMap(Map<? extends K, ? extends V> map)
HashMap的API
//清空整个表
void	clear()

//返回此HashMap实例的浅拷贝:未克隆键和值本身
Object	clone()

//如果表中含有特定key值则返回true
boolean	containsKey(Object key)

//如果表中含有特定value值则返回true
boolean	containsValue(Object value)

//返回键值对的集合
Set<Map.Entry<K,V>>	entrySet()

//返回指定键映射到的值,如果此映射不包含键的映射,则返回null。
V	get(Object key)

//如果此映射不包含键 - 值映射,则返回true
boolean	isEmpty()

//返回HashMap中的键值集合
Set<K>	keySet()

//存入新的键值对
V	put(K key, V value)

//复制特定的Map表
void	putAll(Map<? extends K,? extends V> m)

//移除特定key值的映射
V	remove(Object key)

//返回Map表中映射的数量
int	size()

//返回Map表中的Collection集合
Collection<V>	values()

Hashtable

  • 底层数组+链表实现,无论key还是value都不能为null,线程安全,实现线程安全的方式是在修改数据时锁住整个HashTable,效率低,ConcurrentHashMap做了相关优化
  • 初始size为11,扩容:newsize = olesize*2+1
  • 计算index的方法:index = (hash & 0x7FFFFFFF) % tab.length
Hashtable的构造函数
// 默认构造函数。
public Hashtable() 

// 指定“容量大小”的构造函数
public Hashtable(int initialCapacity) 

// 指定“容量大小”和“加载因子”的构造函数
public Hashtable(int initialCapacity, float loadFactor) 

// 包含“子Map”的构造函数
public Hashtable(Map<? extends K, ? extends V> t)
Hashtable的API
void	clear()
//Clears this hashtable so that it contains no keys.

Object	clone()
//Creates a shallow copy of this hashtable.

boolean	contains(Object value)
//Tests if some key maps into the specified value in this hashtable.

boolean	containsKey(Object key)
//Tests if the specified object is a key in this hashtable.

boolean	containsValue(Object value)
//Returns true if this hashtable maps one or more keys to this value.

Enumeration<V>	elements()
//Returns an enumeration of the values in this hashtable.

Set<Map.Entry<K,V>>	entrySet()
//Returns a Set view of the mappings contained in this map.

boolean	equals(Object o)
//Compares the specified Object with this Map for equality, as per the definition in the Map interface.

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.

int	hashCode()
//Returns the hash code value for this Map as per the definition in the Map interface.

boolean	isEmpty()
//Tests if this hashtable maps no keys to values.

Enumeration<K>	keys()
//Returns an enumeration of the keys in this hashtable.

Set<K>	keySet()
//Returns a Set view of the keys contained in this map.

V	put(K key, V value)
//Maps the specified key to the specified value in this hashtable.

void	putAll(Map<? extends K,? extends V> t)
//Copies all of the mappings from the specified map to this hashtable.

protected void	rehash()
//Increases the capacity of and internally reorganizes this hashtable, in order to accommodate and access its entries more efficiently.

V	remove(Object key)
//Removes the key (and its corresponding value) from this hashtable.

int	size()
//Returns the number of keys in this hashtable.

String	toString()
//Returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space).

Collection<V>	values()
//Returns a Collection view of the values contained in this map.

参考文献:
[1]https://www.jianshu.com/p/034315b201b6
[2]https://www.cnblogs.com/skywang12345/p/3310887.html
[3]https://blog.csdn.net/qq_32635069/article/details/79798741
[4]http://tool.oschina.net/apidocs/apidoc?api=jdk_7u4
[5]https://www.cnblogs.com/heyonggang/p/9112731.html

### 回答1: HashMapHashtable都是Java中用于存储键值对的数据结构,但是它们有一些显著的不同点: 1. 线程安全:Hashtable是线程安全的,而HashMap是线程不安全的。 2. 允许空键和空值:HashMap允许空键和空值,而Hashtable不允许空键和空值。 3. 性能:因为HashMap是线程不安全的,所以在单线程环境中它的性能比Hashtable好。 4. 方法HashMap的put()和remove()方法在操作失败时会返回null,而Hashtable的put()和remove()方法会抛出异常。 5. 使用场景:因为HashMap是线程不安全的,所以它更适合在单线程环境中使用,而Hashtable则更适合在多线程环境中使用。 ### 回答2: HashMapHashtableJava中的两种常用的集合类,它们都实现了Map接口,用于存储键值对。 首先,HashMapHashtable的主要区别在于线程安全性。Hashtable是线程安全的,而HashMap不是。因为Hashtable的所有方法都是同步的,可以在多线程环境下使用,但是这也导致了效率较低。而HashMap则不提供同步,如果在多线程环境下使用需要自己进行同步处理。 其次,HashMap允许将null作为键和值,而Hashtable不允许。这是因为Hashtable使用了Enumeration迭代器,而null值会导致NullPointerException。 另外,HashMap是非线程安全的,但是在大多数情况下比Hashtable效率更高,因为Hashtable需要额外的同步操作。在单线程环境下,推荐使用HashMap,而在多线程环境下需要线程安全的情况下可以使用Hashtable或者ConcurrentHashMap。 此外,HashMap的键对象可以是非线程安全的,但是Hashtable的键对象必须是线程安全的。这是因为Hashtable在计算键的散列值时,会直接使用键对象的hashCode方法,而HashMap则会使用键对象的hashCode方法计算散列值,再通过散列值计算得到数组的下标位置。 综上所述,HashMapHashtable区别主要体现在线程安全性和对null值的处理上。在单线程环境下选择HashMap,在多线程环境下选择线程安全的Hashtable或者ConcurrentHashMap
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值