Performance options for HashSet and HashMap

Source: Internet
Author: User

For HashSet and its subclasses, they use the hash algorithm to determine the storage location of the elements in the set, and the hash algorithm to control the size of the set;
For HashMap, Hashtable and its subclasses, they use the hash algorithm to find the key store in the map, and to increase the size of the key collection by using the hash algorithm.

The hash table can store the location of the element known as "bucket (bucket)", in general, when one element is stored in a single bucket, it has the best performance: the hash algorithm can calculate the storage location of the bucket based on the hashcode value, and then take the element out of the bucket. But the state of the hash table is open: In the case of a hash conflict, a single bucket stores multiple elements that are stored in a linked list and must be searched sequentially.

Because HashSet and HashMap and Hashtable use the hash algorithm to determine the storage of their elements (HashMap only consider key), the HashSet, HashMap hash table contains the following attributes. Capacity (capacity): Number of buckets in a hash table initialization capacity (inital capacity): Number of buckets when the hash table is created. Both HashMap and HashSet allow the initialization capacity to be specified in the constructor. Size: The number of records in the current hash table. Payload factor (load factor): Load factor equals "size/capacity". The load factor is 0, which indicates an empty hash table, and 0.5 represents a half full hash table, and so on. The hash table with light load has fewer conflicts and is suitable for inserting and querying (but it is slower to iterate the elements with iterator).

In addition, the hash table also has a "load limit" is a 0~1 value, "Load Limit" determines the hash list of the maximum fill program.
When the load factor in the hash table reaches the specified "load limit", the hash table automatically multiplies the capacity (the number of buckets) and assigns the original object to the new bucket, which is called rehashing.

The HashSet and HashMap, Hashtable constructors allow you to specify a load limit, hashset and HashMap, and the Hashtable default "load Limit" of 0.75, indicating that when 3/4 of the hash table is already filled, Hash table will occur rehashing.

The default value of Load limit (0.75) is a tradeoff between time and space costs:
A higher "load limit" reduces the amount of memory space used by a hash table, but increases the cost of querying data and time, and queries are the most frequent operations (HashMap's Get () and put () methods use queries);
A lower load limit improves the performance of query data, but increases the memory overhead that the hash table consumes.

If you start to know that HashSet and HashMap, Hashtable save a lot of records, you can use large initialization capacity at creation time, if the initialization capacity is always greater than the maximum number of records contained in HashSet, Hashtable, and HashMap divided by the Load Limit ", there will be no rehashing. When you create HashSet and HashMap, Hashtable with large enough initialization capacity, you can increase the record more efficiently, but setting the initialization capacity too high can be a waste of space, so do not say initialization capacity is too expensive.

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.