Java HashMap Hashcode

Source: Internet
Author: User

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:

    1. Transient entry[] table;
    2. Static class Entry<k,v> implements map.entry<k,v> {
    3. final K Key;
    4. V value;
    5. Entry<k,v> Next;
    6. final int hash;
    7. ......
    8. }

adding elements

    1. Public V put (K key, V value) {
    2. //HashMap allow null keys and null values to be stored.
    3. //When key is null, the Putfornullkey method is called, and value is placed in the first position of the array.
    4. if (key = = null)
    5. return Putfornullkey (value);
    6. //The hash value is recalculated based on the keycode of the key.
    7. int hash = hash (Key.hashcode ());
    8. //Search the index of the specified hash value in the corresponding table.
    9. int i = indexfor (hash, table.length);
    10. //If the Entry at the I index is not NULL, the next element of the E element is traversed continuously through the loop.
    11. For (entry<k,v> e = table[i]; E! = null; e = e.next) {
    12. Object K;
    13. //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.
    14. if (E.hash = = Hash && (k = e.key) = = Key | | key.equals (k)) {
    15. V oldValue = E.value;
    16. E.value = value;
    17. E.recordaccess (this);
    18. return oldValue;
    19. }
    20. }
    21. //If the entry at the I index is null, there is no entry here.
    22. modcount++;
    23. //Add key, value to index at I.
    24. AddEntry (hash, key, value, I);
    25. return null;

Reading elements

  1. Public V get (Object key) {
  2. if (key = = null)
  3. return Getfornullkey ();
  4. int hash = hash (Key.hashcode ());
  5. for (entry<k,v> e = table[indexfor (hash, table.length)];
  6. E! = null;
  7. E = e.next) {
  8. Object K;
  9. if (E.hash = = Hash && (k = e.key) = = Key | | key.equals (k)))
  10. return e.value;
  11. }
  12. return null;
  13. }

String equals

  1. /** 
  2. * Compares this string to the specified object. The result is {@code
  3. * true} if and only if the argument are not {@code null} and is a {@code
  4. * String} object that represents the same sequence of characters as this
  5. * object.
  6. *
  7. * @param anobject
  8. * The object to compare this {@code String} against
  9. *
  10. * @return {@code true} if the given object represents a {@code String}
  11. * Equivalent to this string, {@code false} otherwise
  12. *
  13. * @see #compareTo (String)
  14. * @see #equalsIgnoreCase (String)
  15. */
  16. public Boolean equals (Object anobject) {
  17. if (this= = AnObject) {
  18. return true;
  19. }
  20. if (anobject instanceof String) {
  21. String anotherstring = (string) anobject;
  22. int n = count;
  23. if (n = = anotherstring.count) {
  24. char v1[] = value;
  25. char v2[] = Anotherstring.value;
  26. int i = offset;
  27. int j = Anotherstring.offset;
  28. While (n--! = 0) {
  29. if (v1[i++]! = v2[j++])
  30. return false;
  31. }
  32. return true;
  33. }
  34. }
  35. return false;
  36. }

String hashcode

  1. /** 
  2. * Returns A hash code for this string. The hash code for a
  3. * <code>String</code> object is computed as
  4. * <blockquote><pre>
  5. * s[0]*31^ (n-1) + s[1]*31^ (n-2) + ... + s[n-1]
  6. * </pre></blockquote>
  7. * Using <code>int</code> arithmetic, where <code>s[i]</code> is the
  8. * <i>i</i>th character of the string, <code>n</code> is the length of
  9. * The string, and <code>^</code> indicates exponentiation.
  10. * (The hash value of the empty string is zero.)
  11. *
  12. * @return A hash code value for this object.
  13. */
  14. public int hashcode () {
  15. int h = hash;
  16. int len = count;
  17. if (h = = 0 && len > 0) {
  18. int off = offset;
  19. char val[] = value;
  20. For (int i = 0; i < len; i++) {
  21. H = 31*h + val[off++];
  22. }
  23. hash = h;
  24. }
  25. return h;
  26. }

hash function

  1. /**
  2. * Applies a supplemental hash function to a given hashcode, which
  3. * Defends against poor quality hash functions. This is critical
  4. * Because HashMap uses power-of-two length hash tables, that
  5. * Otherwise encounter collisions for hashcodes this do not differ
  6. * in lower bits. Note:null keys always maps to hash 0, thus index 0.
  7. */
  8. tatic int hash (int h) {
  9. //This function ensures, hashcodes that differ
  10. //constant multiples at each bit position has a bounded
  11. //number of collisions (approximately 8 at default load factor).
  12. H ^= (H >>> ) ^ (H >>> 12);
  13. return H ^ (H >>> 7) ^ (H >>> 4);
  14. }
  15. /** 
  16. * Returns index for hash code h.
  17. */
  18. static int indexfor (int h, int length) {
  19. return H & (length-1);
  20. }

Java HashMap Hashcode

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.