Java源码解析HashMap成员变量
本文基于jdk1.8进行分析
关于HashMap的简介,可以参考这篇文章https://www.nhooo.com/article/154177.htm。
首先看一下HashMap的一些静态常量。第一个是DEFAULT_INITIAL_CAPACITY,默认初始大小,16。从注释中可以了解到,大小必须为2的指数。这里的16,采用的1左移4位实现。而“aka”,是asknownas的缩写。
/** *Thedefaultinitialcapacity-MUSTbeapoweroftwo. **/ staticfinalintDEFAULT_INITIAL_CAPACITY=1<<4;//aka16
接下来是最大容量,当通过任何一个构造函数的参数隐式指明时使用该值。必须是2的指数,且小于等于1<<30,即2的30次方。
/** *Themaximumcapacity,usedifahighervalueisimplicitlyspecified *byeitheroftheconstructorswitharguments. *MUSTbeapoweroftwo<=1<<30. **/ staticfinalintMAXIMUM_CAPACITY=1<<30;
接下来是负载因子,默认值为0.75F。
/** *Theloadfactorusedwhennonespecifiedinconstructor. **/ staticfinalfloatDEFAULT_LOAD_FACTOR=0.75f;
接下来是和红黑树相关的几个常量。在jdk1.8中,如果哈希表中的链表太长,就会转化为一个红黑树。
TREEIFY_THRESHOLD,表示要转为红黑树的最小元素个数,即8。把红黑树转化为链表的门限个数是6. MIN_TREEIFY_CAPACITY为64,表示把链表转化为红黑树的最小元素个数。否则,如果太多节点在一个链表中时,哈希表会扩容,而不会转化为红黑树。
/** *Thebincountthresholdforusingatreeratherthanlistfora *bin.Binsareconvertedtotreeswhenaddinganelementtoa *binwithatleastthismanynodes.Thevaluemustbegreater *than2andshouldbeatleast8tomeshwithassumptionsin *treeremovalaboutconversionbacktoplainbinsupon *shrinkage. **/ staticfinalintTREEIFY_THRESHOLD=8; /** *Thebincountthresholdforuntreeifyinga(split)binduringa *resizeoperation.ShouldbelessthanTREEIFY_THRESHOLD,andat *most6tomeshwithshrinkagedetectionunderremoval. **/ staticfinalintUNTREEIFY_THRESHOLD=6; /** *Thesmallesttablecapacityforwhichbinsmaybetreeified. *(Otherwisethetableisresizediftoomanynodesinabin.) *Shouldbeatleast4*TREEIFY_THRESHOLDtoavoidconflicts *betweenresizingandtreeificationthresholds. **/ staticfinalintMIN_TREEIFY_CAPACITY=64;
接下来是table,它是保存HashMap的最主要的数据结构,如下图。从注释中也可以了解到,table的大小一定是2的指数。
/** *Thetable,initializedonfirstuse,andresizedas *necessary.Whenallocated,lengthisalwaysapoweroftwo. *(Wealsotoleratelengthzeroinsomeoperationstoallow *bootstrappingmechanicsthatarecurrentlynotneeded.) **/ transientNode[]table;
接下来是entrySet,如下图。它保存缓存的映射关系集合。注意,keySet()和values()使用的是父类AbstractMap的属性。
/** *HoldscachedentrySet().NotethatAbstractMapfieldsareused *forkeySet()andvalues(). **/ transientSet>entrySet;
最后是一些其他的属性,包括HashMap中元素个数size,修改次数modCount,下一次进行resize的门限个数,以及负载因子loadFactor,如下图。需要注意的是,loadFactor是final的,也就是说,它一旦被赋值,就不能再修改了。
/** *Thenumberofkey-valuemappingscontainedinthismap. **/ transientintsize; /** *ThenumberoftimesthisHashMaphasbeenstructurallymodified *Structuralmodificationsarethosethatchangethenumberofmappingsin *theHashMaporotherwisemodifyitsinternalstructure(e.g., *rehash).ThisfieldisusedtomakeiteratorsonCollection-viewsof *theHashMapfail-fast.(SeeConcurrentModificationException). **/ transientintmodCount; /** *Thenextsizevalueatwhichtoresize(capacity*loadfactor). *@serial **/ //(Thejavadocdescriptionistrueuponserialization. //Additionally,ifthetablearrayhasnotbeenallocated,this //fieldholdstheinitialarraycapacity,orzerosignifying //DEFAULT_INITIAL_CAPACITY.) intthreshold; /** *Theloadfactorforthehashtable. * *@serial **/ finalfloatloadFactor;
Thisistheend.
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接