Translation Links: http://www.codeceo.com/article/java-hashmap-value-sort.htmlThe English version: How to Sort HashMap Based on the Values in JavaTranslation Author: Code Rural network – Xiao Feng
HashMap in Java is a data structure for storing "key" and "value" information
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
quickly through the hash function.The Hashcode method in Java is to map the object-related information (such as the object's storage address, the object's field, and so on) into a numeric value, which is called a hash, according to certain rules.Back to see, hashcode corresponds to the key code value of keys, and then we put the Java code in the string hashcode calculation code to see:The hashcode is actua
Taobao in the intranet to see colleagues posted a CPU was 100% of the online fault, and this happened many times, because the Java language in the concurrency of the use of HashMap caused race Condition, resulting in a dead loop. This thing I have experienced 4, 5 years ago, I thought there was nothing to write, because Java
array whose value is located by the hash of the key, and then the Equals method to determine whether the value exists.Five otherHash method Finalinthash (objectk) {inth=hashSeed; if (0!=hkinstanceofstring) { returnsun.misc.hashing.stringhash32 ((String) k); }h^=k.hashcode ();// thisfunctionensuresthathashcodesthatdifferonlyby //constantmultiplesateachbitpositionhavea bounded//numberofcollisions (approximately8at Defaultloadfactor) .h^= (h>>>20) ^ (h>>>12);returnh^ (h>>> 7) ^ (hGT;GT;GT;NBSP;4)
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
The original author dripping: http://www.cnblogs.com/eflylab/archive/2007/01/20/625237.htmlJava provides several ways to hold objects, including the built-in array of languages, and the container Class (container classes) provided in utilities, also known as the Cluster Class (collection classes). The collection is very important in Java, and before the discussion, let's look at some of the classic questions in the interview.1 The difference between C
the use and traversal of HashMap in JavaCategory: Algorithms and data Structures 2011-10-19 10:53 5345 people read reviews (0) favorite reports Hashmapjavastringobjectiteratorlist1: A simple example of using HashMap[Java]View Plaincopy
Package com.pb.collection;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Set;
Import J
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
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
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
Problem
Comparison of Java Native
1. TreeMap
2. HashMap
3. ConcurrentSkipListMap
Efficiency of three Map types.
Result
Simulate the insertion and search of massive data within, and test the performance by adding and searching. The results are as follows:
Map type
Insert
Search (within million data records)
10 W
50 W
100 million
150 million
0-1 W
0-25 W
0-50 W
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
This is a classic problem in Java and is often asked in interviews. In fact, a lot of books or articles have mentioned to Overload hashcode () and Equals () two methods to implement a custom key in the HashMap search, but why and if not to do this will have the consequences, as if very little an article, so write an article to explain.
First of all, if we directly use the following person class as a key to
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.