简体   繁体   English

Java集合HashTable和HashMap

[英]Java Collections HashTable and HashMap

为什么在Java中Hashmap允许一个空键,而在Hashtable的情况下不允许呢?

http://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html http://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html

If you look at docs of HashMap 如果您查看HashMap文档

The HashMap class is roughly equivalent to HashTable , except that it is unsynchronized and permits null 's.) HashMap类与HashTable大致等效,除了它是不同步的并且允许使用null 。)

HashTable is the older version of HashMap which failed in that case of handling null 's. HashTableHashMap的较旧版本,在处理null的情况下失败。 And HashMap got that feature added into it to get more advanced than HashTable . 并且HashMap中添加了该功能,以使其比HashTable更高级。

Hash table is very old class , from JDK 1.0 哈希表是非常老的类,从JDK 1.0起

To understand this, first of all you need to understand comments written on this class by author. 要了解这一点,首先,您需要了解作者在该课程上写的评论。

This class implements a hashtable, which maps keys to values. 此类实现哈希表,该哈希表将键映射到值。 Any non-null object can be used as a key or as a value. 任何非空对象都可以用作键或值。 To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. 为了成功地从哈希表存储和检索对象,用作键的对象必须实现hashCode方法和equals方法。

HashTable class is implemented on hashing mechanism, that's mean to store any key-value pair, its required hash code of key object. HashTable类是在哈希机制上实现的,意味着存储任何键值对,即键对象所需的哈希码。 If key would be null, it will not able to given hash ,it will through null pointer exception and similar case for value it is throwing null if the value is null. 如果key为null,它将无法给出哈希值,它将通过null指针异常和类似的情况返回值,如果value为null,则抛出null。

But later on it was realized that null key and value has its own importance that is why one null key and multiple null values are allowed in later implemented classes like HashMap class. 但是后来人们意识到空键和值具有其自身的重要性,这就是为什么在后来实现的类(例如HashMap类)中允许一个空键和多个空值的原因。

For hash map null keys will allow and there is a null check is there for keys if the key is null then that element will be stored in a zero location in Entry array. 对于哈希映射,空键将允许,如果键为空,则对键进行空检查,然后将该元素存储在Entry数组中的零位置。 null key we can use for some default value.. 空键,我们可以使用一些默认值。

HashMap allows the null key. HashMap允许使用null键。 If you try to insert the another value of same key, it will override it. 如果您尝试插入相同键的另一个值,它将覆盖它。

Incase of HashTable, put(K key, V value) throws the Null pointer Exception if the key or value is null. 对于HashTable,如果键或值为null,则put(K key,V value)会引发Null指针异常。

Refer the source code. 请参考源代码。 HashMap: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/util/HashMap.java#HashMap.put%28java.lang.Object%2Cjava.lang.Object%29 HashMap: http : //grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/util/HashMap.java#HashMap.put%28java.lang.Object%2Cjava。 lang.Object 29%

HashTable: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Hashtable.java#Hashtable.put%28java.lang.Object%2Cjava.lang.Object%29 哈希表: http : //grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Hashtable.java#Hashtable.put%28java.lang.Object%2Cjava。 lang.Object 29%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM