Java Foundation--java Collection (iv)

Source: Internet
Author: User

HashSet class

HashSet is a typical implementation of the set interface, which is used most of the time when a set collection is used. HashSet stores the elements in the collection by a hash algorithm, so it has good access and lookup capabilities.

HashSet has the following characteristics:

    • The order of elements cannot be guaranteed, and the order may differ from the order in which they are added. The order is also subject to change.
    • HashSet is not synchronous, and if more than one thread accesses a hashset at the same time, assuming that there are two or more threads modifying the HashSet collection at the same time, it must be synchronized by code.
    • The collection element value can be null

When an element is deposited into the HashSet collection, HashSet calls the object's Hashcode () method to get the Hashcode value of the object, and how the object is stored in hashcode based on the HashSet value. If there are two elements that return true through the Equals () method, but their hashcode () methods return values that are not equal, HashSet will still be able to add them in a different location.

Simply put, the hashset set determines that the two-element equality criterion is that two objects are compared by the Equals () method, and that the return value of the Hashcode () method of two objects is equal.

The following program provides three classes A, B, C, respectively, they rewrite the Equals (), Hashcode () Two methods of one or all, through this program can see HashSet judge set elements of the same criteria.

Import java.util.*;//the Equals method of Class A always returns true, but does not override its Hashcode () methodclassa{ Publicboolean equals (Object obj) {return true; }}    //the Hashcode () method of Class B always returns 1, but does not override its Equals () methodclassb{ Public inthashcode () {return 1; }}    //the Hashcode () method of Class C always returns 2 and has the override of its Equals () methodclassc{ Public inthashcode () {return 2; }     Publicboolean equals (Object obj) {return true; }} Public classhashsettest{ Public Static voidMain (string[] args) {HashSet books=NewHashSet (); //add two A objects, two B objects, and two C objects to the Books collection, respectivelyBooks.add (NewA ()); Books.add (NewA ()); Books.add (NewB ()); Books.add (NewB ()); Books.add (NewC ()); Books.add (NewC ()); System. out. println (books); }}

See the following running results:

[Email protected]1, [email protected]1, [email protected]2, [email protected]26114629 , [email protected]]

as can be seen from the above program, even though the two a elements return true through the Aquals () method, HashSet still treats them as two objects, even though the hashcode () of the two B objects return the same value (all 1), but HashSet still treat them as two objects.

Java Foundation--java Collection (iv)

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.