Java Review-hashmap and Weakhashmap

Source: Internet
Author: User
Tags whm

HashMap key and value values are strong references, meaning that the HashMap object is not destroyed when the key value pair is not destroyed, but Weakhashmap, when the inside of the key value is not destroyed when the words may be automatically destroyed by the Java recycling mechanism

Weakhashmap whm=new weakhashmap<> ();
Whm.put (New String ("PHP"), "75 Yuan");
Whm.put (New String ("Java"), "90 Yuan");
System.GC ();
System.runfinalization ();
System.out.println (WHM);
HashMap whm1=new HashMap ();
Whm1.put (New String ("PHP"), "75 Yuan");
Whm1.put (New String ("Java"), "90 Yuan");
Forcing the system to be garbage collected
System.GC ();
System.runfinalization ();
System.out.println (WHM1);

{}
{php=75 yuan, java=90 yuan}

There are two ways to determine whether two variables are equal in Java: One is to use the = = operator, and the other is to take advantage of the Equals method. If the variable is a basic type, then using = = and using equals results in the same result, which determines whether the value of the variable is relative. If the variable is a reference type, the = = operator Determines whether the variable points to the same reference object, and equals determines whether the variable "value" is equal. The wrapper class for the Java value type (Boolean, Byte, short, Integer, Long, Float, Double, charcater) can be judged by the = = operator.



  1. int inta = 65;
  2. float floata = 65.0f;
  3. Integer INTB = 65;
  4. Float FLOATB = 65.0f;
  5. INTA = = Floata According to the value, the result is true
  6. System.out.println ("Inta = = Floata:" + (Inta = = Floata));
  7. INTA = = INTB According to the value, the result is true
  8. System.out.println ("Inta = = Intb:" + (Inta = = INTB));
  9. INTA = = FLOATB According to the value, the result is true
  10. System.out.println ("Inta = = FLOATB:" + (Inta = = FLOATB));
  11. String stra = "Test";
  12. String strb = new String ("test");
  13. String strc = new String ("test");
  14. String STRD = "Test";
  15. Stra and Strb point to different objects and the result is false
  16. System.out.println ("stra = = strb:" + (Stra = = STRB));
  17. Stra and Strd point to the same object, and the result is true
  18. System.out.println ("stra = = strd:" + (Stra = = STRD));
  19. STRB and Strc point to different objects and the result is false
  20. System.out.println ("STRB = = strc:" + (STRB = = strc));
  21. STRB and STRC are the same values, the result is true
  22. System.out.println ("Strb equals strc:" + (Strb.equals (STRC)));
  23. Stra and STRB are the same values, the result is true
  24. System.out.println ("Stra equals STRB:" + (Stra.equals (STRB)));

Java Review-hashmap and Weakhashmap

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.