HASHMAP is not thread safe
The following figure shows the realization of HashMap's Get method, which is judged by the hashcode and Equals method.
It is possible to implement the equality criteria for custom object by overriding the Hashcode and equals methods of the class, with the complete code as follows
Book.java
Package com.shc.map;
public class Book {
private String title;
Public book (String t
The value of HashMap is not sequential, it is implemented according to the hashcode of key. How are we going to achieve this sort of unordered hashmap? (TreeMap similar)Import Java.util.arraylist;import java.util.arrays;import Java.util.collections;import Java.util.Comparator;import Java.util.hashmap;import Java.util.list;import Java.util.map;import java.util.Map.Entry; mapOperation Result:e:55555d:44444c:3
1. In the array we index the content by the array subscript, and in the map we index the object by object, the object to index is called key, and its corresponding object is called value. Two methods of 2.HASHMAP traversal, either through the Set1. Traversing through the keyset collection set hashmap.keyset (); Iterator ketset.iterator (); while (Keyiterator.hasnext ()) { object = keyiterator.next ();
Class:public class HashMap
Inherited from Abstractmap implements the Map,cloneable,serializable interface
Can be serialized
Can be clone
//默认初始容量为16static final int DEFAULT_INITIAL_CAPACITY = 1 InitializationMap
New Hashmap
Initialcapacity to 0xB
Loadfactor defaults to 0.75
The first step is to determine if the value of initialcapacity is less than 0 or greater tha
relationships, it returnsNULL. (ReturnNULLIt may also indicate that the mapping was preceded by aNULLwith theKeyAssociation. )7.putAllpublic void Putall (mapSpecifies that all mappings of mappings are copied to this map8.removePublic V Remove (Object key);removes the mapping of the specified key (if any) from this map. The return value iswith theKeythe old value of the association;Keydoes not have any mapping relationships, it returnsNULL. (ReturnNULLIt may also indicate that the mapping was pr
Array
Array (array) and vector are very similar Java artifacts (constructs), which are completely different and should be determined according to their functions when choosing to use them.
1, array: Java arrays elements can not be subscript out of bounds, to a large extent, to ensure the security of Java programs, and other languages appear this problem often lea
This is a creation in
Article, where the information may have evolved or changed.
Performance comparison
Insert 10 million records to compare memory usage size and insertion time.
Test machine configuration
Intel (R) Core (TM) i3-4000m CPU @2.40ghz 2.40GHz
12.0GB Memory
Front-facing conditions
Go, avoid the memory copy when expanding capacity, the size of the initialization volume is 13333333. Make (map[int]int,13333333)
Java, avoid memory copy when e
= = Hash ((k= e.key) = = Key | | (Key! =NULLKey.equals (k)))) returne; } while((E = e.next)! =NULL); } } return NULL; }1. First determine if table[] is empty, determine whether the table[index is empty by hashing the index, or null if any of the entries is empty.2. Determine table[index] hash and key are all the same as the lookup, if the hash and key are the same, then directly return to the table[index].if // Always Check first node null key.equals (k
HashMap>>>: Move right^: Bitwise XOR OR: Logic andFinal V putval (int hash, K key, V value, Boolean onlyifabsent, nbsp; Boolean evict) { nodeeach time you put an element into HashMap, a node object is new, and the position is determined by calculating the hashcode value of the key. If two elements have the same hashcode, they will be placed on the same index. The problem arises, how do you put it? Originall
temporary e = p;As for the other situation is the position conflict, that is, according to (N-1) hash algorithm has a collision, again sub-situation discussion;1. Deposit in the form of a linked list;2. If a collision causes the chain list to be too long (greater than or equal to Treeify_threshold), convert the linked list into a red-black tree;
Finally, if E is not empty, add e to the table (E.value is assigned the parameter value in Putval ());
The code is as follows:Final V putval
longer executed.(2) But in the case of a hash collision, the first two conditions are established, and then the last equals must be used to determine the equality of the objects.A 3.hash collision scene?(1) Generally appears in large data situations(2) The generation method of Hashcode is weak (such as the artificial production hashcode above)Summary: This chapter mainly through the introduction of hash collision once again in-depth understanding of hashmap
// studnet Package yzhou.gen03; Public class Student{ private T score; Public T Getscore () { return score; } Public void SetScore (T score) { this. score = score; }} // bjstu public class Student{ private T score; public T Getscore () { return score; public void SetScore (T score) { this . Score = SCO Re }}Package Yzhou.gen03;import Java.util.hashmap;import java.util.map;import java.util.set;import Java.util.Map.Entry;/** * generics
. Delete // When you delete a node, you need to // 1. The successor of the forward node points to the subsequent node to delete the node // privatevoid remove () { = after ; = before; }3. Increase Private void Addbefore (entry existingentry) {after = existingentry; The successor node of the current node points to the new node = Existingentry.before; this ; this ; // }4. The rewritten init
3. Longest Substring without repeating charactersGiven a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb" , the answer "abc" is, which the length is 3.Given "bbbbb" , the answer "b" is, with the length of 1.Given "pwwkew" , the answer "wke" is, with the length of 3. Note that the answer must was a substring, is "pwke" a subsequence and not a substring.Problem:Given a string, the length of the longest substring is found without repeating char
The example in this article describes how Java uses the HashMap cache to save data. Share to everyone for your reference, specific as follows:
Private static final Hashmapif (value = = null) {
scache.put (Index, D);
} else {
d = value;
}
}} return D;
}
More about Java algorithms Interested readers can view the site top
A simple operation method of subclass HashMap of map CollectionImport Java.util.hashmap;import Java.util.iterator;public class Hashmapdemos {public static void%r0main (string[] args) {//HashmapClass studenti{private String name;private int age; Studenti (String name, int age) {this.name=name;this.age = age;} public int getage () {return age;} Public String GetName () {return name;} Public String toString () {return ' The Student Name: ' +name + ' and
In HashMap, if you need to use multiple attribute combinations as keys, you can combine the several attributes into one object as key. However, the problem is that, to do get, there is often no way to save the original put operation of the key object reference, at this time, you need to have key object cover the following hashcode() and equals( Object obj) is implemented. Sample code is as follows: Public classTestkeyobject {Private LongID; Private in
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.