A probe into the principle of comparison, hash and collectionutils intersection of Java wrapper classes

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/gklifg/article/details/45914169

1. Applicable and non-applicable scenarios for even (= =) comparisons

Scenario 1:

<pre name= "code" class= "java" >public void Testjava () {Long LongA = new Long (4l); Long LONGB = (longA-2) * *; System.out.println ("longa=" +longa+ ", hash=" +longa.hashcode ()); <span></span>system.out.println (" Longb= "+longb+", hash= "+longb.hashcode ()); System.out.println (LONGA==LONGB);}

Results:

Longa=4,hash=4

Longb=4,hash=4false

Scenario 2:

public void Testjava () {Long LongA = new Long (4l); Long longb = new Long (4l); System.out.println ("longa=" +longa+ ", hash=" +longa.hashcode ()); <span style= "White-space:pre" ></span> System.out.println ("longb=" +longb+ ", hash=" +longb.hashcode ()); System.out.println (LONGA==LONGB);}

Results:

Longa=4,hash=4
Longb=4,hash=4
False

Scenario 3:

public void Testjava () {Long LongA = 4l;<span style= "White-space:pre" ></span>long longb = 4l; System.out.println ("longa=" +longa+ ", hash=" +longa.hashcode ()); <span style= "White-space:pre" ></span> System.out.println ("longb=" +longb+ ", hash=" +longb.hashcode ()); System.out.println (LONGA==LONGB);}
Results:

Longa=4,hash=4
Longb=4,hash=4
True

Scenario 4:

public void Testjava () {<span style= "white-space:pre" ></span>long LongA = Long.parselong ("4"); <span Style= "White-space:pre" ></span>long longb = Long.parselong ("4"); <span style= "White-space:pre" ></ Span><span style= "White-space:pre" ></span>system.out.println ("longa=" +longA+ ", hash=" + Longa.hashcode ()); <span style= "White-space:pre" ></span>system.out.println ("longB=" +longB+ ", hash=" + Longb.hashcode ()); <span style= "White-space:pre" ></span>system.out.println (LongA==longB);}

Results:

Longa=4,hash=4
Longb=4,hash=4
True

Scenario 5:

public void Testjava () {Long LongA = 4L;//JVM automatically assigns long longb = new Long (4);//Manually create long LONGC = Long.parselong ("4");//? Long Longd = (4-2) *2l;//? Long longe = (LongA + 4)/2;//? Long LONGF = (longb + 8)/3;//? System.out.println ("A==c:" + (LONGA==LONGC)); System.out.println ("A==d:" + (LONGA==LONGD)); System.out.println ("B==c:" + (LONGB==LONGC)); System.out.println ("B==d:" + (LONGB==LONGD)); System.out.println ("C==d:" + (LONGC==LONGD)); System.out.println ("A==e:" + (Longa==longe)); System.out.println ("B==e:" + (Longb==longe)); System.out.println ("A==f:" + (LONGA==LONGF)); System.out.println ("B==f:" + (LONGB==LONGF));}
Results:
A==c:true
A==d:true
B==c:false
B==d:false
C==d:true
A==e:true
B==e:false
A==f:true
B==f:false

Conclusion: As long as it is not the manual new type Long value other ways, including the Parselong method, the resulting object is the JVM automatically allocated objects.


2.HashSet de-weight mechanism

HashSet is actually a hashmap encapsulation, so the mechanism inherits HashMap's practice, and HashMap's hash function relies on ojbect.hashcode (), from the above example can be known, the same value of the packaging class hashcode is the same, So all long values of the same value will be weighed down. The object that is put in set is left, and the others are discarded. If A and B are reversed in order, then the set leaves B.

public void Testjava () {Long LongA = 4L;//JVM automatically assigns long longb = new Long (4);//Manually create long LONGC = Long.parselong ("4");//? Long Longd = (4-2) *2l;//? Long longe = (LongA + 4)/2;//? Long LONGF = (longb + 8)/3;//? set<long> longset = new hashset<long> () Longset.add (LongA);//Note These two lines longset.add (LONGB); <span style= " Font-family:arial, Helvetica, Sans-serif; >//Note These two lines </span>longset.add (LONGC); Longset.add (LONGD); Longset.add (longe); Longset.add (LongF); System.out.println (Longset.size ()); for (Long L:longset) {System.out.println ("A==l:" + (longa==l)); System.out.println ("B==l:" + (Longb==l));}}
Results:

1
A==l:true
B==l:false

3.collectionutils.intersection () de-weight rule:

The source code is as follows:

public static Collection intersection (Final Collection A, final Collection b) {ArrayList list = new ArrayList ();        Map Mapa = Getcardinalitymap (a),//key is an element of a, value is the number of occurrences of the element, the same as map MAPB = Getcardinalitymap (b);        Set ELTs = new HashSet (a); Elts.addall (b); <span style= "font-family:arial, Helvetica, Sans-serif;"        >//element after hash to re-set </span> Iterator it = Elts.iterator (); while (It.hasnext ()) {Object obj = It.next (); <span style= "White-space:pre" ></span>//For each element, if a or b There are no secondary elements, then skip, if there are several, then put the "number of smaller party" element <span style= "White-space:pre" ></span>//Can see, <span style= " Font-family:arial, Helvetica, Sans-serif; > No matter how many of the same objects before the intersection, as long as they hash uniformly,</span> into the result set is the same object for (int i=0,m=math.min (Getfreq (Obj,mapa), Getfreq (obj,            MAPB)); i<m;i++) {list.add (obj);    }} return list; }

The source can see Apache Collectionutils.intersection () is also dependent on the Hashcode (Mapa, MAPB are hashmap), so the basic class of packaging can be assured that the weight, Don't worry about the problem of not having to go to a different object address.


A probe into the principle of comparison, hash and collectionutils intersection of Java wrapper classes

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.