Java set interface

Source: Internet
Author: User

HashSet Collection is a subclass of the set interface

The underlying is a hash table that cannot store duplicate elements, and guarantees data uniqueness through the Hashcode method and the Equals method. There is no guarantee that the order of data is consistent with the order in which data is stored.

The Hashcode method is used to compare the hash value of 2 elements, and if the hash value is equal, call the Equals method to compare the contents of the element

What is a hash table?

The bottom of the hash table is also the array mechanism, the array also holds the object, and these objects are placed in the array in the location of the special, when the need to put these objects to the array, then according to the specific data of these objects combined with the corresponding algorithm, the object in the array to calculate the position, and then put this object in the array. And such an array is called a hash array, that is, a hash table.

When the hash value is the same, the content is stored under the same hash list when it is different.

1  Public Static voidmethod01 () {2Hashset<string>Set=NewHashset<string>();3         Set. Add ("ABC");4         Set. Add ("ABC");5System. out. println (Set);6}

HashSet Storing custom elements

Give When you store a custom type element in HashSet, you need to override the hashcode and Equals methods in the object to establish your own comparison method to guarantee that the objects in the HashSet collection are unique

1  Public Static voidmethod03 () {2Hashset<person>Set=NewHashset<person>();3         Set. Add (NewPerson ("a", -));4         Set. Add (NewPerson ("b", -));5         Set. Add (NewPerson ("C", +));6          for(Iterator it =Set. iterator (); It.hasnext ();) {7System. out. println (It.next ());8         }9}
1 Package com.orcale.demo01;2 3  Public classPerson {4     PrivateString name;5     Private  intAge ;6      PublicPerson () {};7      PublicPerson (String name,intAge ) {8          This. Name =name;9          This. Age =Age ;Ten     } One      PublicString GetName () { A         returnname; -     } -      Public voidsetName (String name) { the          This. Name =name; -     } -      Public intGetage () { -         returnAge ; +     } -      Public voidSetage (intAge ) { +          This. Age =Age ; A     } at      PublicString toString () { -         return "Person [Name="+ name +", age="+ Age +"]"; -     } -      Publicboolean equals (Object obj) {//Overrides the Equals method to determine whether the 2 values are the same, and then determine if the value is empty,
If it is not the case that the value is not the person class, because it is the Obj class parent class, all to be transformed downward, and then determine whether the value is the same - if( This==obj) { - return true; in } - if(obj==NULL){ to return false; + } - if(obj instanceof person) { thePerson p=(person) obj; * returnName.equals (p.name) &&age==P.age; $ }Panax Notoginseng return false; - } the + Public inthashcode () { AFinalintPrime = to; the intresult =1; +result = Prime * result +Age ; -result = Prime * result + ((name = =NULL) ?0: Name.hashcode ()); $ returnresult; $ } -}

The Linkedhashset collection is a subclass of the HashSet collection, and the order in which he pulls the data is consistent with the order in which the data is stored, and the data is unique.

1  Public Static voidmethod05 () {2Linkedhashset<integer>Set=NewLinkedhashset<integer>();3         Set. Add (1);4         Set. Add (2);5         Set. Add (3);6          for(Integer I:Set){7System. out. println (i);8         }9}

Java set interface

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.