"Java" HashSet

Source: Internet
Author: User

1  PackageCom.tn.hashSet;2 3  Public classPerson {4     Private intID;5     PrivateString name;6     PrivateString birthday;7      PublicPerson (intID, string name, string birthday) {8         Super();9          This. ID =ID;Ten          This. Name =name; One          This. birthday=birthday; A     } -      Public intgetId () { -         returnID; the     } -      Public voidSetId (intID) { -          This. ID =ID; -     } +      PublicString GetName () { -         returnname; +     } A      Public voidsetName (String name) { at          This. Name =name; -     } -      PublicString Getbirthday () { -         returnbirthday; -     } -      Public voidsetbirthday (String birthday) { in          This. Birthday =birthday; -     } to @Override +      PublicString toString () { -         returnID + "-" +name; the     } * @Override $      Public inthashcode () {Panax Notoginseng         Final intPrime = 31; -         intresult = 1; theresult = Prime * result +ID; +         returnresult; A     } the @Override +      Public Booleanequals (Object obj) { -         if( This==obj) $             return true; $         if(obj = =NULL) -             return false; -         if(GetClass ()! =Obj.getclass ()) the             return false; -person other =(person) obj;Wuyi         if(id! =other.id) the             return false; -         return true; Wu     } -}
Person.java
1  PackageCom.tn.hashSet;2 3 ImportJava.util.HashSet;4 ImportJava.util.Iterator;5 6  Public classHashsettest {7     /*8 * The bottom of the hashset is HashMap9 * Note:Ten * 1. A unique identifier must be determined based on the actual situation to override the hashcode and Equals methods One * 2. Once the object is placed in the HashSet container, the unique identifier can no longer be modified or removed.  A * 3.HashSet Object Location and object Hashcode algorithm wait for the value correlation, find remove all need to use the hashcode algorithm - * value, if the unique identifier is modified then the HASHCODE algorithm value will not be the same as before, the object position is changed, resulting in the object cannot be found, in the long- - * causes memory leaks.  the      */ -      Public Static voidMain (string[] args) { -Hashset<person> persons=NewHashset<person>(); -Person p1=NewPerson (1001, "Zhang San", "1990-1-1"); +Person p2=NewPerson (1002, "John Doe", "1988-2-2"); -Person p3=NewPerson (1003, "Harry", "1989-3-3"); + Persons.add (p1); A Persons.add (p2); at Persons.add (p3); -          -SYSTEM.OUT.PRINTLN (persons);//that is, the return value of ToString for each object in the print container -          -         //foreach Traversal -          for(person p:persons) in System.out.println (p); -                  to         //iterator Traversal +Iterator<person> iterator=persons.iterator (); -          while(Iterator.hasnext ()) the System.out.println (Iterator.next ()); *          $         /*You cannot use for traversal because Haseset has no get method. Panax Notoginseng for (int i=0;i<persons.size (); i++) { - System.out.println (Persons.get (i)); the         }*/ +          A         //HashSet cannot aggravate complex objects the System.out.println (Persons.size ()); + Persons.add (p3); - System.out.println (Persons.size ()); $          $         /* - * If you do not rewrite the hashcode and equals method for person P4 and P3 will be judged by the Equals method of object because of different address - * Two different objects, if overridden by hashcode and equals, is considered to be the same object.  the          */ -Person p4=NewPerson (1003, "Harry", "1989-3-3");Wuyi          the Persons.add (p4); -System.out.println (Persons.size ());//do not override Hashcode and equals print 4, override print 3 Wu          -         Booleanb; Aboutb=Persons.remove (p1); $ System.out.println (b); - System.out.println (Persons.size ()); -P2.setid (1000);//The object Unique identifier in the HashSet container is changed, resulting in hashcode and equals algorithm values that are not the same as before -B=persons.remove (p2);//the removal failure directly returns false, and the program does not error.  A System.out.println (b); + System.out.println (Persons.size ()); the     }     -}
Hashsettest.java

"Java" HashSet

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.