Java programming ideology Reading Notes-4 (Chapter 4-Working Principle and Implementation of 2HashMap)

Source: Internet
Author: User

4. Implement a simple HashMap and Its Principle
4.1 In the put () method:
1) First, the key-value pair hash code to be inserted is obtained through the key, and the hash code is used as the index to find the elements corresponding to the key in the array bucket.
2) encapsulate the key-value pair to be inserted into an object of the class implementing the Map. Entry interface.
3) in operation 1) check whether there are elements that are the same as the key-value pair to be inserted in the array element (also a sort list). If yes, update the key-value pair array element to be inserted if no value exists.
4.2 In the get () method
1) First, obtain the key-value pair hash code to be searched through the key, and use the hash code as the index to find the elements corresponding to the key in the array bucket.
2) encapsulate the key-value pair to be searched into an object of the class implementing the Map. Entry interface.
3) in operation 1) check whether there are elements that are the same as the key-value pair to be inserted in the array element (also a sort list). If yes, return the value corresponding to the key. If no value exists, return a null value.
4.3 one instance

  1. ImportJava. util .*;
  2. /**
  3. * The MPair class implements Map. Entry.
  4. */
  5. ClassMPair
  6. Implements Map. Entry,Comparable{
  7. ObjectKey, value;
  8. MPair (ObjectK,ObjectV ){
  9. Key = k;
  10. Value = v;
  11. }
  12. Public ObjectGetKey (){ReturnKey ;}
  13. Public ObjectGetValue (){ReturnValue ;}
  14. Public ObjectSetValue (ObjectV ){
  15. ObjectResult = value;
  16. Value = v;
  17. ReturnResult;
  18. }
  19. /**
  20. * When comparing two MPair objects, their key values are compared.
  21. */
  22. Public BooleanEquals (ObjectO ){
  23. ReturnKey. equals (MPair) o). key );
  24. }
  25. Public IntCompareTo (ObjectRv ){
  26. Return(((Comparable) Key). compareTo (MPair) rv). key ));
  27. }
  28. }
  29. ClassSimpleHashMapExtends AbstractMap{
  30. Private Final Static IntSZ = 997;
  31. Private Shortlist[] Bucket =New Shortlist[SZ];
  32. /**
  33. * Encapsulate the key and value into the implementation class of Map. Entry and insert it into the array.
  34. */
  35. Public ObjectPut (ObjectKey,ObjectValue ){
  36. ObjectResult =Null;
  37. // Obtain the key-value pair hash code to be inserted through the key
  38. IntIndex = key. hashCode () % SZ;
  39. If(Index <0) index =-index;
  40. If(Bucket [index] =Null)
  41. Bucket [index] =New Shortlist();
  42. & Nb

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.