allow null valueswhere key and value are objects and cannot contain duplicate keys, but can contain duplicate value.In Hashtable, both key and value do not allow null values.In HashMap, NULL can be used as a key with only one key, and one or more keys can have a value of NULL. When the Get () method returns a null value, the key may not be in HashMap, or the value corresponding to the key may be null. Ther
ContainsKey three methods, where contains and Containsvalue function the same.IV, whether key and value allow null values.Where key and value are objects and cannot contain duplicate keys, but can contain duplicate value.In Hashtable, both key and value do not allow null values.In HashMap, NULL can be used as a key with only one key, and one or more keys can have a value of NULL. When the Get () method returns a null value, the key may not be in
Recent code often appears in some cache caches to reduce the number of user requests resulting in high data server load, and these cache caches of the underlying implementation of the data structure support are map, and decided to look at the following map of the source code. So I started from HashMap. The Entry class source code in the map Entry HashMap used in 1. Ha
It seems that map inherits from Collection like other Collection classes. In fact, Map and Collection do not have any relationship at the structural level, by viewing the source code, we can find that all map operations are based on key-value pairs, rather than individual elements.
The following uses HashMap as an example to gain an in-depth understanding of the implementation mechanism of Map. In this proc
Differences between Map and HashMap, Hashtable, and HashSet in Java, hashmaphashsetHashMap and Hashtable both implement the Map interface, both save the K-V pair (key-value Pair); HashSet implements the Set interface, the nature is similar to the Set.
Differences between HashTable and HashMap1. Different Hashtable inherited from the Dictionary class and HashMap
write or write it.What's the difference with Java8 's hashmap?There are hash collisions, such as two hashes that fall on the same index after modulo, or two different keys that have the same hash value.JDK7 's practice is to build a linked list, and then insert the elements on top of each to perform the above judgment.And JDK8 in the chain table length reached 8, and the number of barrels reached 64, built a red black tree, to solve the serious confl
Cause
Today in the project encountered a very "wonderful" problem. This is roughly the case: Android Terminals and servers (Spring), exactly the same string key values in the HashMap into the order is not the same, which directly led to the server and the Android terminal using the HMACSHA256 algorithm to encrypt the digest is not the same, The server will not be able to perform proper data validation.
Then with depressed mood to the program plus br
, value, I); // Add new elements, key!Return NULL; // return without the same key value}
The key methods are analyzed as follows:
Void addentry (INT hash, object key, object value, int bucketindex ){Table [bucketindex] = new entry (hash, key, value, table [bucketindex]);
Because the hash algorithm may make different key values have the same hash code and have the same table index, for example: the hash values of key = "33" and key
This article reproduced others, the original address: http://www.xyczero.com/blog/article/16/CauseToday in the project encountered a very "wonderful" problem. This is basically the case: Android terminal and Server (Spring), the exact same string key value to put in the HashMap in order, which directly led to the server and Android terminal with the HmacSHA256 algorithm encryption of the digest is not the same, The server will not be able to verify th
① Inherit differentpublic class Hashtable extends Dictionary implements maps public class HashMap extends Abstractmap implements mapThe methods in ②hashtable are synchronous, and the methods in HashMap are not synchronized by default.In the context of multi-threaded concurrency, you can use Hashtable directly, but to use hashmap, you need to increase the synchron
Hash calculation is the element in which the computing element should be placed in the array. It is accurate to the linked list to which it is placed. According to Java rules, if you want to put an object into hashmap, your object class must provide the hashcode method and return an integer. For example, the string class has the following method:
[
to synchronize No isK,v can null whetherHashMap is a lightweight implementation of Hashtable (non-thread-safe implementation), they all complete the map interface,The main difference is that HASHMAP allows null (NULL) key values (key), which may be more efficient than Hashtable due to non-thread safety.HashMap allows NULL to be used as a entry key or value, and Hashtable is not allowed.HashMap hashtable contains method removed, changed to Containsval
.Hashtable supports iterator and enumeration two ways of traversing . 5) Different initial capacity and expansion modethe default capacity of the HashMap is twice times the capacity of the original, when expanding;the default capacity of the Hashtable is one, and the capacity is changed to twice timesthe original +1.6) The hash value algorithm for adding key-value is differentHashMap When adding elements, a custom hashing algorithm is used ;Hashtable
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 hashtable contains method removed, change
The analysis of a storage class is nothing more than two points: the data structure used for storage and the operating mechanism of storage.
Data structure:
Array
[Java]/*** The table, resized as necessary. Length MUST Always be a power of two.*/Transient Entry [] table;
/*** The table, resized as necessary. Length MUST Always be a power of two.*/Transient Entry [] table;Entry
Linked List format.
That is, HashMap
The HashMap is implemented through a entry array. The entry structure has three properties, Key,value,next. If in C, we encounter next think of the inevitable pointer, actually in Java this is a pointer. Hashes store data each time through the value of the hashcode. and Hashcode () The simplest method of breaking the law is:
hash algorithm in string:
public int hashcode () {
int h = hash;
First recognition HashMapBased on the implementation of the map interface of a hash table (that is, a hash table), this implementation provides all the optional mapping operations and allows NULL values and NULL keys to be used.HashMap inherits from Abstractmap, and realizes the map, cloneable, java.io.Serializable interface. and is not synchronized, meaning it is not thread-safe.Data structure of HashMapIn the Java programming language, the most basi
HashMap for Java Collection Framework LearningThe following features of HashMap can be obtained from the API documentation: implemented based on the hash table, and the chain hash table allows null values and null keys (null = null key-value pairs) hashMap and Hashtable are basically the same. The difference is that
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.