Simple analysis of Java Foundation---->hashset (i)

Source: Internet
Author: User

For HashSet, it is implemented based on HashMap, and the underlying uses HashMap to preserve the elements. Today we will simply analyze the implementation of it.

Simple analysis of HashSet--the composition of member variables of HashSet
 Public class extends Implements Set<e>, cloneable, java.io.Serializableprivatetransient hashmap<e,object> map; // Dummy value to associate with a Object in the backing Map Private Static Final New Object ();

HashSet is constructed by creating a hashmap.

 Public HashSet () {    new hashmap<>();}

Second, some operations of HashSet are based on the HashMap created above
 PublicIterator<e>iterator () {returnMap.keyset (). iterator ();} Public intsize () {returnmap.size ();} Public BooleanIsEmpty () {returnmap.isempty ();} Public Booleancontains (Object o) {returnMap.containskey (o);} Public BooleanAdd (e e) {returnMap.put (e, PRESENT) = =NULL;//present is a member variable of HashSet} Public BooleanRemove (Object o) {returnMap.Remove (o) ==present;//present is a member variable of HashSet} Public voidClear () {map.clear ();}

Friendship Link
    • The principle of HashMap:http://www.cnblogs.com/huhx/p/baseusejavahashmap1.html

Simple analysis of Java Foundation---->hashset (i)

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.