Java random 4-hashset

Source: Internet
Author: User
Package COM. broadengate. fangxing; import Java. util. arraylist; import Java. util. collection; import Java. util. hashset;/*** it should be known that hashset is not allowed to be put into duplicate data. So what is it based on to determine whether it is a duplicate data? * Hash is used in hashset. Algorithm Because the hashset underlying layer also uses hashmap to access data *, while the hashmap underlying layer uses arrays to access data, and hashcode * is used to store data in hashmap *, this is the reason why the data stored by hashmap is not sequential. It is stored Based on hashcode. ** if hashcode is not overwritten, the object is used by default, it is a hashcode generated based on the object's reference address. Therefore, when no object is created, its reference address is different, **/public class hashcodetest {public static void main (string [] ARGs) {hashcodetest = new hashcodetest (); Collection coll = new/* arraylist () */hashset (); person = hashcodetest. New person ("1", 1); person person1 = hashcodetest. new person ("1", 1); person person2 = hashcodetest. new person ("1", 1); person person3 = hashcodetest. new person ("1", 1); person person4 = hashcodetest. new person ("1", 1); person person5 = hashcodetest. new person ("1", 1); Coll. add (person5); Coll. add (person4); Coll. add (person); Coll. add (person2); Coll. add (person3); Coll. add (person1); // If the hashcode and EQ methods are overwritten, if Change the value of the object in the memory, and the hashcode value will change. // The JVM cannot find the person object in the region calculated based on the hashcode, therefore, the person cannot be removed. setage (2); Coll. remove (person); system. out. println (Coll. size ();} class person {private string name; private int age; Public Person () {} public person (string name, int age) {This. name = Name; this. age = age;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} public int getage () {re Turn age;} public void setage (INT age) {This. age = age;}/*** @ override public int hashcode () {final int prime = 31; int result = * 1; Result = prime * result + getoutertype (). hashcode (); * result = prime * result + age; Result = prime * result + * (name = NULL )? 0: Name. hashcode (); return result;} * @ override public Boolean equals (Object OBJ) {If (this = OBJ) return * true; If (OBJ = NULL) return false; If (getclass ()! = * Obj. getclass () return false; Person Other = (person) OBJ; * If (! Getoutertype (). Equals (other. getoutertype () return * false; If (age! = Other. Age) return false; If (name = * null) {If (other. Name! = NULL) return false;} else if *(! Name. Equals (other. Name) return false; return true;} ** private hashcodetest getoutertype () {return * hashcodetest. This ;}**/}}

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.