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
the HashMap key, and string is most commonly used because the string is immutable and final, and the Equals () and Hashcode () methods have been overridden. Other wrapper classes also have this feature. Immutability is necessary, because in order to compute hashcode (), the key value should be prevented from changing
2. Adhere to the rules of overriding equals () and Hashcode () methods when using custom objects as keys to ensure that different objec
Java-HashMap analysis, javahashmap
(1) Hash Algorithm
Hash algorithm, which maps the original data to a shorter, fixed-length binary value through the hash function.
The hash algorithm has two basic features: repeatable and irreversible. The hash value calculated theoretically can be repeated, but this is basically not the case for good hash functions. Irreversible means that the hash value cannot be used t
created here.So how did the user set the capacity hashmap initialized? Use the new HashMap (int capacity) constructor:Finally, the constructor is called, and the load factor is the default value. There is no new array, but a critical value is computed. In the previous picture Resize () method, if the table has a size of 0 and Newthr is greater than 0, the array is created according to the size of newthr. T
First of all, Hashmap,hashmap is a class, and all classes in Java inherit from an object class. The Hashcode () method is defined in the object class, in other words, any class will have this hashcode () method.So the Key.hashcode () function calls the hash function with the key key value type and returns the int hash value.First look at the two methods defined i
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
The difference between HashMap, HashSet, and Hashtable is a common interview topic for Java programmers, which is only recorded in this blog and is thoroughly analyzed in the source code:Before the analysis, the difference is listed below1:hashset the bottom of the HashMap implementation, but there is no key-value, only the H
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
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
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
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
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
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.