) { ... Find a power of 2 >= initialcapacity int capacity = 1; while (capacity Note that the table initial size is not initialcapacity!! in the constructorBut >= initialcapacity 2 of the power of n!!!!Why are ———— so designed? ——3. Ways to resolve hash conflicts
Open addressing Method (linear detection re-hash, two-time detection and re-hash, pseudo-random detection and hashing)
Re-hash method
Chain Address method
Create a public overflow zone
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 sorted List interface │ ordered Li
. The rehash operation does not occur if the initial capacity is greater than the maximum number of entries divided by the load factor.The HashMap constructorHashMap a total of 4 constructors , as follows:// The default constructor. HashMap ()// Specify the constructor for "Capacity size" HashMap (int capacity)// Constructors that specify the capacity size an
JAVA Study Notes (22)-set HashMap and Hashtable, hashmaphashtableHashMap class
Import java. util. hashMap; import java. util. map;/** HashMap class, which implements the Map interface and stores data key-value */public class Test0
Map is an important data structure, and this article will show you how to use different maps, such as hashmap,treemap,hashtable and Linkedhashmap.MapOverviewThere are four common map implementations in Java, Hashmap,treemap,hashtable and Linkedhashmap, and we can use a sentence to describe each map as follows:
HASHMA
Java Collection List, Set, Map summary + HashMap/Hashtable difference, hashmaphashtableList: (ordered, can be repeated) through the subscript index ---- ArrayList variable array, random search ---- random List linked List, insert and delete at any location faster ---- Vector efficiency is lower than arraylist, but can be used for multi-thread synchronizationSet :( unordered, not repeated) a set can have a m
the Map interface have a predictable sequence of iterations. The difference between this implementation and HASHMAP is that Linkedhashmap maintains a two-way loop linked list. This list defines the order of the iterations, which is usually the order in which the elements will be held.It is important to note that if you re-deposit the key in the map, the position of the key will not change, just replace the value.Summarize:The difference between
rewrite it in its hashcode (). There are two principles for rewriting hashcode ():You do not have to produce a unique hashcode for each different object, as long as your Hashcode method enables get () to get the contents of the put () to be put in. That is, "not a principle". The algorithm that generates hashcode as far as possible to make the value of hashcode scattered some, not many hashcode are concentrated in one range, this is advantageous to improve
HashMap uses the data structure, the jargon "list hash", the code is real /** * 定义了一个Entry的数组用来存储数据。 */ transient Entry[] table; ' Java/** * Entry is an internally defined class */Static class Entry
Transient is implemented in a class that is serializable, and is ignored when attributes that are identified by transient are serialized. if (loadfactor Returning true means that
0. PrefaceWork busy, a long time without looking at the algorithm, even the DFA sensitive word algorithm to be optimistic for a while to understand ... is really and third-order Rubik's Cube reduction, the pastoral will be Wu, very pity ah.In the DFA algorithm, the first step is to understand its data structure, on this basis, it involves some hashmap assignment. The assignment here is very interesting, with three
When it comes to hashmap, people who use the Java language are more familiar with it. Just talk about the hashmap we know today.First, let's look at the HashMap class in Java Public class extends Abstractmapimplements map, cloneable, Serializable { private staticfinallon
, although their contents are the same, but they have different addresses in memory. So it is natural that the above procedure is not going to be the result of our vision. The following changes are made to the element class:Class element{ int number; public Element (int n) { number=n; } public int hashcode () { return number; } public boolean equals (Object o) { return (o instanceof Element) (number== (Eleme
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
the length expands, the hash rules change as well.
Let's review the hash formula:index = hashcode (Key) (Length-1)
When the original array length is 8 o'clock, the hash operation is done with the 111B (representing the binary 7), and the new array length is 16,hash and 1111B (representing the binary 15). hash results are obviously different .
The Java code for rehash is as follows:
/**
* Transfers all entries from the current table to newtable.
Java defines an interface java.util.Map for the mappings in the data structure; it has four implementation classes, namely HashMap Hashtable Linkedhashmap and TreeMapThe map is primarily used to store the health-value pairs, and the key is not allowed to repeat (overwriting is repeated), but allows the values to be duplicated.Hashmap is the most commonly used map, it stores data according to the hashcode va
Java Collection Framework HashSet and HASHMAP source Analysis
General Introduction
The reason why we put HashSet and hashmap together, because they have the same implementation in Java, the former is only a layer of packaging, that is, hashset inside a HashMap (adapter mod
Comparison in the Java world, regardless of class or data, the structure of the process is the logic of the entire program and the key to performance. Because I contacted a question about the coexistence of performance and logic, I began to study this problem. Searched the forum of big and Small, also "Java Virtual machine Specification", "apress,.java.collections." (2001),. Bm.ocr.6.0.shareconnector ", and
, HashMap is the first time put into the element, only the capacity of the HashMap and other information to initialize, see below) init (); }Here you can see what the HashMap constructor does:1, HashMap container, default capacity 16, expansion load factor 0.752. The constructor only makes the parameter check and param
Java HashMap implementation principle source code analysis, javahashmap
HashMap is implemented based on the Map interface of the hash table. It provides all the optional ing operations and allows creation of null values and null values. It is not synchronized and the ing sequence is not guaranteed. Next we will record the implementation principles of
, which is not a bug.Data structure of HashMapHashMap mainly use arrays to store data, we all know that it will hash the key, the HA operation will have a duplicate hash value, for the hash value of the conflict, HashMap using the linked list to solve.
In HashMap, there is a property declaration that says:Transient entry[] table;Entry is the class that the HashMap
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.