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.
Hashmap sorting
1. sort by key
For map sorting in Java, there is a Sort map, such as treemap. For this map, you can only sort by key first, and then sort by put and remove, performance may be compromised.
In this solution, hashmap is used for creation and addition. If you want to sort by key, you can pass this hashmap
its hash function, any object that is a key must implement the Hashcode and Equals methods. The hashcode and Equals methods inherit from the root class object, and if you use a custom class as a key, be very careful, as defined by the hash function, if two objects are the same, that is, Obj1.equals (OBJ2) =true, Their hashcode must be the same, but if two objects are different, their hashcode is not necessarily different, and if the hashcode of two d
In-depth analysis and comparison of HashMap in Java-Linux general technology-Linux programming and kernel information. The following is a detailed description. In the world of Java, the structure processing of classes and various types of data is the key to the logic and performance of the entire program. As I encountered a problem that the performance and logic
Java basics: parsing HashMap and HashSet
I. HashMap
HashMap stores the data structure referenced by the "Key-Value" Object Based on the hash table.
The saved key must have two key functions:
(1) equals (): determines whether the two keys are the same to ensure the uniqueness of the saved Key;
(2) hashCode (): Calcula
? ——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
The solution to HashMap in Java is to use the chain address approach.4. Re-hashing the rehash processWhen the hash table capacity exceeds the default capacity, t
, an improved hash formula moves out of the sign bit, and then computes the remainder with the modulo (or%) operator.(123 0x7FFFFFFF)% 20 = 3(456 0x7FFFFFFF)% 20 = 16This ensures that you get a positive index value. If you look at the Java 8 HashMap source code, its implementation uses the following methods:a). prevent undesirable discrete values (poorer hashes) by extracting only important lows.b). The i
Turn from:Java HashSet and HashMap source analysisGeneral IntroductionThe reason why HashSet and HashMap together is explained, because they have the same implementation in Java, the former is only to the latter to do a layer of packaging, that is to say HashSet inside there is a HashMap(adapter mode). Therefore, this
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 in Java is a key-value pair (Key-value) that stores elements. HashMap requires a hash function that uses the hashcode () and Equals () methods to add and retrieve elements to the collection/collection. When the put () method is called, HashMap calculates the hash value of the key and stores the key-value pair o
rehash can not be collected immediately. Then why not in the HashMap larger than the expansion of a little bit, I do not have a very satisfactory answer to this question, I only know that the hash of the modulo operation using a bitwise operation, the bitwise operation needs to limit the length of the array must be 2 exponent. In addition, Java heap memory is used in the tcmalloc of the library, their allo
In the world of Java, the structure processing of classes and various types of data is the key to the logic and performance of the entire program. As I encountered a problem that the performance and logic coexist at the same time, I began to study this problem. I searched for forums, large and small, and also read the Java Virtual Machine specification, apress ,. java
In the world of Java, the structure processing of classes and various types of data is the key to the logic and performance of the entire program. As I encountered a problem that the performance and logic coexist at the same time, I began to study this problem. I searched for forums, large and small, and also read the Java Virtual Machine specification, apress ,. java
In the world of Java, the structure processing of classes and various types of data is the key to the logic and performance of the entire program. As I encountered a problem that the performance and logic coexist at the same time, I began to study this problem. I searched for forums, large and small, and also read the Java Virtual Machine specification, apress ,. java
Java improvement (two or three)-----HASHMAPHashMap is also the implementation of a hash table-based MAP interface that we use very much collection, which exists in the form of Key-value. In HashMap, Key-value is always treated as a whole, and the system calculates the storage location of Key-value based on the hash algorithm, and we can always save and fetch value quickly by key. The following is an analysi
The important nature and optimization of HashMap in java, javahashmap
Preface
As HashMap plays an important role in java Development, it is particularly important to summarize some important attributes and optimization points of hashmap, at the same time, it can also improve
This article hashmap the realization of the source code for a simple analysis. The version information of the HashMap source used is as follows:/* * @ (#) Hashmap.java 1.73 07/03/13** Copyright 2006 Sun Microsystems, Inc. All rights reserved.* SUN proprietary/confidential. Use are subject to license terms. */I. OverviewEach object in Java has a hash code, which c
hierarchy . Collection represents a set of objects, also known as Collection elements . Some collection allow duplicate elements, while others do not. Some of the collection are orderly, while others are unordered. The JDK does not provide any direct implementation of this interface: it provides more specific sub-interfaces (such as Set and List) implementations. This interface is often used to pass collection and operate these collection where it is required to be the most universal.Collection
in synchronized block...Synchronized (m) {// Synchronizing on m, not s!Iterator I = s. iterator (); // Must be in synchronized blockWhile (I. hasNext ())Foo (I. next ());}
3. In HashMap, null can be used as a key, and there is only one such key. One or more keys can correspond to null values. When the get () method returns a null value, it can indicate that the key does not exist in HashMap or that the cor
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.