I. Overview of HASHMAPHashMapis one of the most commonly used collection class frameworks in Java and is a very typical data structure in the Java language. The HashMap is implemented on a hash table-based map interface, which provides all the optional mapping operations. Stored is thetheA mapping that allows multiple null values and a null key. However, this cla
Java Collection framework 08 -- HashMap and source code analysis1. Introduction to HashMap
First, let's take a look at the inheritance relationship of HashMap.
java.lang.Object ? java.util.AbstractMap
? java.util.HashMap
public class HashMap
quickly index key?
2. Principle
First, let's look at a simple hashmap implementation in "Thinking in Java":
: Containers/simplehashmap.java//A demonstration hashed Map.
Import java.util.*;
Import net.mindview.util.*; public class Simplehashmap
Simplehashmap constructs a hash table to store the Key,hash function is the modulo operation Math.Abs (Key.hashcode ())% SIZE, uses the link table method
The recent interview was asked about the principle of hashmap in Java, and suddenly speechless, so the lesson from the bitter feeling research.hashcode and equals in Java1, about Hashcode
The existence of hashcode is mainly used to find the shortcut, such as Hashtable,hashmap, etc., Hashcode is used in the hash storage structure to determine the storage
.
HashMap is a data structure that supports fast access, and you must understand its data structure to understand its performance.
Third, data structure
We know that two of the most commonly used structures in Java are arrays and analog pointers (references), and almost all data structures can be combined to implement these two, as is the case with HashMap. In
same key, and each key can only map one value. The map interface provides three sets of views. The map content can be treated as a set of keys, a set of values, or a set of key-value ing.Hashmap classHashmap is similar to hashtable. The difference is that hashmap is non-synchronous and allows null, that is, null value and null key ., However, when hashmap is treated as a collection (the values () method ca
Java container HashMap and HashSet Learning
In Java learning, I saw the HashMap and HashSet classes. I learned the JDK source code and recorded the notes at the usage level (many companies have to ask the underlying layer for interviews.
Src.zip under source code jdk1.7
HashMap
are important parameters that affect the performance of the HashMap, where capacity represents the number of buckets in the hash table, the initial capacity is the capacity to create a hashtable, and the load factor is the size of the hash table before its capacity is automatically increased by a scale, which measures how much space is used for a hash list, The larger the load factor, the higher the filling of the hash table, and the smaller the inve
Entry[newCapacity]; //将原有数组转到新的数组中 transfer(newTable); table = newTable; //重新设置阈值,为新的容量和负载因子的乘积 threshold = (int)(newCapacity * loadFactory);}Where the array migration logic is mainly implemented in the transfer () function, the function is implemented and annotated as follows:voidTransfer (entry[] newtable) {entry[] src = table;intnewcapacity = Newtable.length;//Iterate through all the table entries in the array for(intj =0; J //When the table item index has a value exists,
:
If a Thread-safe implementation is not needed, it's recommended to use HASHMAP in place of Hashtable. If a thread-safe highly-concurrent implementation is desired and then it's recommended to use Java.util.concurrent.Concurren Thashmap in place of Hashtable.
Simply put, if you don't need thread safety, then use HASHMAP, if you need thread safety, then use Concurrenthashmap. Hashtable has bee
elements), and then compute the array subscript, and finally in Createentry () using the head interpolation method to insert the element.
Get operation
Public V get (Object key) {
if (key = null) return
getfornullkey ();
entry
This is simpler than put (), which also determines whether the key is null and then the traversal query of the linked list.
Performance optimization HashMap is an efficient and versatile data structure th
.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 custo
Float.isnan (loadfactor))Throw NewIllegalArgumentException ("Illegal load factor:" +loadfactor); //Find a power of 2 >= initialcapacity//you need to pay attention here . intCapacity = 1; while(Capacity initialcapacity) Capacity; //setting the load factor This. Loadfactor =Loadfactor; //set the next critical value for expansionThreshold = (int) Math.min (capacity * Loadfactor, maximum_capacity + 1); //initializing a hash tableTable =NewEntry[capacity]; Usealthashing= sun.misc.VM.isBooted
0. Foreword
The difference between HashMap and Hashtable a relatively simple answer is:
(1) HashMap is not thread-safe, Hashtable is thread-safe.
(2) HashMap's keys and values allow NULL to exist, while Hashtable is not.
(3) HashMap efficiency is higher than hashtable because of the problem of thread safety and hash efficiency.
But if you keep asking: What is an
Java interview questions: how to sort HashMap key values
In Java, HashMap is a data structure used to store "key" and "value" Information pairs. Unlike Array, ArrayList, and lists, it does not maintain the order of inserted elements.Therefore, sorting HashMap Based on keys
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
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
Read the HashMap (1) and javahashmap of Java source code with me.
Recently, I am very busy. I want to learn and discuss some Java source code and its implemented Data Structure with you,
Not a high level of things. If you are interested, take a look.
1. Why Map? Use HashMap as an example.
In many cases, we need to stor
you can search for the correct list. A Data Structure like this is called a hash table (hashtable ).HashTables in JavaJava contains two classes: Java. util. hashtable and Java. util. hashmap. They provide a hashtable mechanism for multiple purposes. These two classes are very similar and generally provide the same public interface. But they do have some importan
IncreaseDeleteThe Remove method allows you to delete a specified element.Find*************************************************HashSet can neither use the index of 0 1 2 3 to determine the value of an element, nor use key to determine the value of an element*********************HashSet like this ["AA", "BB"] grow into such a data structure******************HashMap grow into such {a=123, B=hello}Jsonobject very much like {"A": 123, "B": null}
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.