1.HashMap
"1" Traversal: HashMap unordered traversal
For (map.entry<string, String>m:map.entryset ()//key-value pairs, allow the key to be empty String
{
System.out.println (M.getkey () + ":" +m.getvalue ());
}
HashMap Source:
- Transient entry[] table;
- Static class Entry<k,v> implements map.entry<k,v> {
- final K Key;
- V value;
- Entry<k,v> Next;
- final int hash;
- ......
- }
adding elements
- Public V put (K key, V value) {
- //HashMap allow null keys and null values to be stored.
- //When key is null, the Putfornullkey method is called, and value is placed in the first position of the array.
- if (key = = null)
- return Putfornullkey (value);
- //The hash value is recalculated based on the keycode of the key.
- int hash = hash (Key.hashcode ());
- //Search the index of the specified hash value in the corresponding table.
- int i = indexfor (hash, table.length);
- //If the Entry at the I index is not NULL, the next element of the E element is traversed continuously through the loop.
- For (entry<k,v> e = table[i]; E! = null; e = e.next) {
- Object K;
- //If a entry hash of the linked list at the I index is found to be equal to the hash of the new entry and the key is the same, the new entry overwrites the old entry and returns.
- if (E.hash = = Hash && (k = e.key) = = Key | | key.equals (k)) {
- V oldValue = E.value;
- E.value = value;
- E.recordaccess (this);
- return oldValue;
- }
- }
- //If the entry at the I index is null, there is no entry here.
- modcount++;
- //Add key, value to index at I.
- AddEntry (hash, key, value, I);
- return null;
Reading elements
- Public V get (Object key) {
- if (key = = null)
- return Getfornullkey ();
- int hash = hash (Key.hashcode ());
- for (entry<k,v> e = table[indexfor (hash, table.length)];
- E! = null;
- E = e.next) {
- Object K;
- if (E.hash = = Hash && (k = e.key) = = Key | | key.equals (k)))
- return e.value;
- }
- return null;
- }
String equals
- /**
- * Compares this string to the specified object. The result is {@code
- * true} if and only if the argument are not {@code null} and is a {@code
- * String} object that represents the same sequence of characters as this
- * object.
- *
- * @param anobject
- * The object to compare this {@code String} against
- *
- * @return {@code true} if the given object represents a {@code String}
- * Equivalent to this string, {@code false} otherwise
- *
- * @see #compareTo (String)
- * @see #equalsIgnoreCase (String)
- */
- public Boolean equals (Object anobject) {
- if (this= = AnObject) {
- return true;
- }
- if (anobject instanceof String) {
- String anotherstring = (string) anobject;
- int n = count;
- if (n = = anotherstring.count) {
- char v1[] = value;
- char v2[] = Anotherstring.value;
- int i = offset;
- int j = Anotherstring.offset;
- While (n--! = 0) {
- if (v1[i++]! = v2[j++])
- return false;
- }
- return true;
- }
- }
- return false;
- }
String hashcode
- /**
- * Returns A hash code for this string. The hash code for a
- * <code>String</code> object is computed as
- * <blockquote><pre>
- * s[0]*31^ (n-1) + s[1]*31^ (n-2) + ... + s[n-1]
- * </pre></blockquote>
- * Using <code>int</code> arithmetic, where <code>s[i]</code> is the
- * <i>i</i>th character of the string, <code>n</code> is the length of
- * The string, and <code>^</code> indicates exponentiation.
- * (The hash value of the empty string is zero.)
- *
- * @return A hash code value for this object.
- */
- public int hashcode () {
- int h = hash;
- int len = count;
- if (h = = 0 && len > 0) {
- int off = offset;
- char val[] = value;
- For (int i = 0; i < len; i++) {
- H = 31*h + val[off++];
- }
- hash = h;
- }
- return h;
- }
hash function
- /**
- * Applies a supplemental hash function to a given hashcode, which
- * Defends against poor quality hash functions. This is critical
- * Because HashMap uses power-of-two length hash tables, that
- * Otherwise encounter collisions for hashcodes this do not differ
- * in lower bits. Note:null keys always maps to hash 0, thus index 0.
- */
- tatic int hash (int h) {
- //This function ensures, hashcodes that differ
- //constant multiples at each bit position has a bounded
- //number of collisions (approximately 8 at default load factor).
- H ^= (H >>> ) ^ (H >>> 12);
- return H ^ (H >>> 7) ^ (H >>> 4);
- }
- /**
- * Returns index for hash code h.
- */
- static int indexfor (int h, int length) {
- return H & (length-1);
- }
Java HashMap Hashcode