"Thinking in JAVA" and online data collation:The use of Hashtable in Java:
Java contains two classes, java.util.Hashtable and JAVA.UTIL.HASHMAP, which provide a multi-purpose Hashtable mechanism. The Hashtable and HashMap object
you can search for the correct list. A Data Structure like this is called a hash table (hashtable ).HashTables in JavaJava contains two classes: Java. util. hashtable and Java. util. hashmap. They provide a hashtable mechanism for multiple purposes. These two classes are ve
.2, Hashtable and HashMap different places:There are three important differences between the Hashtable and the HashMap class. The first difference is mainly historical reasons. Hashtable is based on the old dictionary class ,HashMap is an implementation of the map interface introduced by Java 1.2 .Perhaps the most impo
:
If a Thread-safe implementation is not needed, it's recommended to use HASHMAP in place of Hashtable. If a thread-safe highly-concurrent implementation is desired and then it's recommended to use Java.util.concurrent.Concurren Thashmap in place of Hashtable.
Simply put, if you don't need thread safety, then use HASHMAP, if you need thread safety, then use Concurrenthashmap.
OverviewLike HashMap, Hashtable is also a hash table that stores content that is a key-value pair.Hashtable is defined in Java as: Public class Hashtableextends dictionaryimplementsFrom the source, we can see that Hashtable inherited from the Dictionary class, the implementation of the Map, cloneable, java.io.Serializable interface. Where the dictionary class is
this is called a hash table (hashtable ).
HashTables in JavaJava contains two classes: Java. util. hashtable and Java. util. hashmap. They provide a hashtable mechanism for multiple purposes. These two classes are very similar and generally provide the same public interfac
).
JavaHashTables inJavaContains two classes,Java. Util. hashtable andJava. Util. hashmap, which provides a multi-purpose hashtable mechanism. These two classes are very similar and generally provide the same public interface. But they do have some important differences. I will talk about them later.
The hashtable and
Hashtable is the implementation of the data structure of key values in Java. You can use the key to store and retrieve the value, which is The identifier that stores the value . Obviously the "key" should be unique. Java.util.Hashtable expands dictionary and implements map. An object with a non- null value can be used as a key or a value. The Hashtable
Hashtable and HashMap are fairly easy to ask in the Java interview, and even become the most frequently asked questions in the collection framework interview, so don't forget to prepare the question before you take any Java interview.Let's first look at the definition of 2 classes. Public class Hashtable extends
: "Hunan"-"Sichuan"-"Anhui"-"Beijing" and "Hunan"-"Anhui"-"Sichuan"-"b Eijing ". The following is the result of Hashtable after running the sample code:In the implementation code of Hashtable, there is a method called rehash to augment the capacity of the Hashtable. Obviously, when the rehash method is called, each key value changes to the corresponding index, wh
Ontains (object value) synchronized Boolean ContainsKey (Object key) synchronized Boolean CONTAINSVA Lue (Object value) synchronized enumeration
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/
2nd part Hashtable Source analysis
To get a better idea of the Hashtable principle, the following is an analysis of t
. Loadfractor plays the role of a flag bit, which marks the beginning and end of the calculation. About the H + + Entry.hashcode () in the H will overflow problem, above the hash () function has seen each time the hash will be with the 0x7fffffff, that is, the hash is always positive, will not overflowSerialization and deserializationprivate void WriteObject (Java.io.ObjectOutputStream s) throws IOException {entryNote that during deserialization, it does not recreate a
0. Foreword
The difference between HashMap and Hashtable a relatively simple answer is:
(1) HashMap is not thread-safe, Hashtable is thread-safe.
(2) HashMap's keys and values allow NULL to exist, while Hashtable is not.
(3) HashMap efficiency is higher than hashtable because of the problem of thread safety and hash e
value of key is obtained by hash () method.2, according to the hash value to get index3. Iterate the linked list, return the corresponding value of the matching key, and return NULL if not found Hashtable traversal modeThere are 4 ways to traverse the Hashtable: 1. Use keys () enumerationThe difference between Hashtable and HashMap
Original address: http://www.javaresearch.org/article/showarticle.jsp? Column = 545 Thread = 17460
Hash table is an important storage method and a common retrieval method. The basic idea is to take the value of the link code as the independent variable, calculate the corresponding function value through a certain functional relationship, and interpret this value as the storage address of the node, store the node to the storage unit corresponding to the storage address. The key code retrieval me
elements. and pay attention to get (), if we pass "roof" as a key, it produces the index number of the Keys.index.Of (), and then uses that index number to generate the value within the relevant value vector.The test in main () is very simple; it simply converts lowercase characters to uppercase characters, which is clearly useful in more efficient ways. But it reveals to us the powerful function of Assocarray.The standard Java library contains only
Arraylist and hashmap are asynchronous, while vector and hashtable are synchronous. Therefore, vector and hashtable are thread-safe, while arraylist and hashmap are not thread-safe.Because synchronization takes machine time, the execution efficiency of vector and hashtable is lower than that of arraylist and hashmap. collection interface implementation class l
Because many years ago just learn Java when used to Hashtable time is more curious why the second T is lowercase, this does not conform to the Sun's style ah, the entire JDK is the standard hump, so with this question over a lot of books, see a lot of information, the final conclusion is:Hashtable's second T-lowercase is Sun's fault, many books mention the problem, many people have suggested to Sun to revis
HashMap is a lightweight implementation of Hashtable (non-thread-safe implementation), they all complete the map interface, the main difference is that the HASHMAP allows null (NULL) key value (key), because of non-thread security, the efficiency may be higher than Hashtable.HashMap allows NULL to be used as a entry key or value, and Hashtable is not allowed.HashMap has
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.