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
hash inthash =Hash (Key.hashcode ()); //calculate the array of small labels I inti =indexfor (hash, table.length); for(entryNULL; E =e.next) {Object k; if(E.hash = = Hash (k = e.key) = = Key | |Key.equals (k))) { returnE.value; } } return NULL; } Private voidAddEntry (intHash, K key, V value,intBucketindex) {EntryTable[bucketindex]; //inserting new elements into the front of the listTable[bucketindex] =NewEntry(hash, key, value, E); if(size++ >=th
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
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
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
-Wxhm.put ("The Pride of the lake", array1); AWxhm.put ("The Condor Man", array2); + the //Add a collection of martial arts novels to the collection -Hm1.put ("Martial arts Fiction", WXHM); $ the //Traverse the the //get a collection of keys for a novel collection theSetHm1.keyset (); - //Traverse in for(String hm1key:hm1set) { the //the value of the novel collection is Hashmap
is a segment--->Segment---> A hashentry---> In the future each Key-value pair is converted into Hashentry object.The structure of--->hashentry is also a unidirectional linked list structure---> This class takes advantage of the CAS operation and the lock method provided by the unsafe class. Achieve thread safety and efficiency. The so-called segmented lock technique is to store elements in multiple containers, each with its own lock. Distribute the pressure of a single lock to multiple locks. G
[Java learning notes] HashMap subinterface --- LinkedHashMap, javalinkedhashmapFeatures:
The order of stored elements is the same as that of retrieved elements (similar to the sequence hashset)
1 import java.util.HashMap; 2 import java.util.Iterator; 3 import java.util.LinkedHashMap; 4 import java.util.Map; 5 6 public class LinkedHashMapDemo { 7 8 public static void main(String[] args) { 9
The first is the use of the HashMap EntrySet () method: map The second is to use HashMap's keyset () method: map In both ways, the efficiency of the former is higher than that of the latter, mainly because the former actually puts both the key and the value into the iterator, and only executes once to find all the key-value pairs. and the latter equivalent to execute two times, the first time the key and value are deposited into the itera
string and the number of occurrences of the character +HashmapNewHashmap(); - //4. Iterate through the character array to get each character and store the characters in a double-column collection + for(CharC:arr) { A //5, the stored procedure to make a judgment, if the collection does not contain this key, the character as a key, the value of 1 storage, if the collection contains this key, the value is added 1 storage at /*if (!hm.containskey (c)) {//If
ObjectiveAs an efficient map structure, HASHMAP has become an indispensable and efficient way to execute in Java.Inheritance relationshipHashMap inherits the Abstractmap, realizes the map interface, cloneable,serailizable interface.Base properties
Default_initial_capacity=16
Maximum_capacity=1
default_load_factor=0.75
Treeify_threshold=8
Untreeify_threshold=6
Min_treeify_capacity=64
NodeThe node class implements the Map.entry int
of space and time consumption?
1, the original when we expand the container of the hash structure, the hash table to new a larger array, and then copy the contents of the original array to the new array, and to hash;
2. It is also a matter of knowledge as to the size of this larger new array, which, in general, will be set to a prime number similar to the double size of the original array. NET in the generation of this prime number there is a certain skill.
From 1 and 2, it can be seen that
Code:1 ImportJava.util.*;2 3 Public classtest{4 5 Public Static voidMain (string[] args)6 {7 8HashMap HM =NewHashMap ();9Hm.put (NULL,NULL);TenHm.put (NULL,NULL); OneHm.put ("a",NULL); A System.out.println (HM); - - } the}Output:{null=null, a=null}Analysis:HashMap can use NULL as key or value, and Hashtable cannot.HashMap thread is unsafe, high efficiency, hashtable thread safety, low efficiency. Analogy ArrayList (unsafe) and vector (safe).Note that you can use the Collections 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.