Small white analysis of Java focus--on HashMap and Hashtable

Source: Internet
Author: User

This is an interview often encounter the knowledge point, no matter what company this knowledge point is almost the test of small white must, why? Because this thing tete common, often see you write 100 lines of code, can use several times, do not ask this question which. So! This small white snare world HashMap and Hashtable knowledge focus is just to deal with the interviewer all kinds of magic question way.

The most common is to ask them the difference:

In fact, the API gives a fairly brief introduction, in the introduction of the HashMap chapter, the original word is said

The implementation of a hash table-based Map interface. This implementation provides all the optional mapping operations and allows null values and null keys to be used. (TheHashMap class is roughly the same as Hashtable except for non-synchronous and null-allowed use.) This class does not guarantee the order of the mappings, especially because it does not guarantee that the order is constant.

hashmap<k,v> hashtable<k,v>extends dictionary<k,v>implements map<k,v>, cloneable, Serializable

Well, you see the difference in fact no more than three points, (spit groove, unexpectedly is enclosed in parentheses, and this unexpectedly became the focus), OK.

1th: Non-synchronous, do not think much, already said the content from HashMap, of course, is hashmap non-synchronous.

2nd: Allow null, in the API, HASHMAP either the key or the value can be null, and Hashtable gives "any non-null object can be its key or value."

3rd: Inheriting classes are different. HashMap inherit from Abstractmap, Hashtable inherit from dictionary.

4th: This is derived from the 1th, non-synchronous means non-thread-safe, and Hashtable is thread-safe, but the processing efficiency, hashmap better than Hashtable.

The difference is almost as far as the future is found to fill in:

Small white analysis of Java focus--on HashMap and Hashtable

Related Article

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.