Java collection classes

Source: Internet
Author: User

1: Why is the hashmap capacity a power of 2? What is a load factor?

Capacity: the number of barrels.

Load factor: Number of elements/capacity.

is the power of 2 because the elements are more evenly distributed after they have been added to the bucket.

2: Why is string, integer such as wrapper class suitable as key?

Because it is immutable, final, and re-writes the hash () and Equals (). Immutable is necessary: To calculate hashcode, prevent the value of key from changing. The class you define can be a key to the map if it satisfies this requirement.

The 3:HASHMAP default capacity is 16, and the default reload factor is 0.75.

4:hashset VS TreeSet?

|--sortedset Interface--treeset Implementation class
Set interface-|--hashset Implementation class
|--linkedhashset Implementation Class

HashSet: does not guarantee that the order/not synchronous/maximum can put a null

When an element is deposited into the hashset, HashSet calls Hashcode () to determine where to store it.

The method of comparing the equality of two objects in HashSet is to compare the hashcode () values of the two objects for equality, if the two objects are not equal if they are not equal, to continue calling equals if the hashcode of the two objects are equal () The method further determines whether two objects are equal if the Equals () method returns true to think that two objects are equal and returns false to consider the two objects unequal.

TreeSet is an ordered set of elements in ascending order, the default is to sort by natural order, meaning that elements in the TreeSet to implement the comparable interface, we can construct the TreeSet object, passing the comparator object that implements the comparator interface.

HashSet is based on the hash algorithm , and its performance is usually better than treeset, and we should usually use HashSet.

Use HashMap when we need to determine whether it is equal; Sort of function when the door was used treeset;

Java collection classes

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.