资源描述:
《Java语言程序设计与数据结构 基础篇原书第11版PPT及其他教辅资源21slide.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1Chapter21SetsandMaps2ObjectivesTostoreunordered,nonduplicateelementsusingaset(§21.2).ToexplorehowandwhentouseHashSet(§21.2.1),LinkedHashSet(§21.2.2),orTreeSet(§21.2.3)tostoreelements.Tocompareperformanceofsetsandlists(§21.3).Tousesetstodevelopaprogramthatcountsthekeywords
2、inaJavasourcefile(§21.4).TotellthedifferencesbetweenCollectionandMapanddescribewhenandhowtouseHashMap,LinkedHashMap,andTreeMaptostorevaluesassociatedwithkeys(§21.5).Tousemapstodevelopaprogramthatcountstheoccurrenceofthewordsinatext(§21.6).Toobtainsingletonsets,lists,andmap
3、s,andunmodifiablesets,lists,andmaps,usingthestaticmethodsintheCollectionsclass(§21.7).3MotivationsThe“No-Fly”listisalist,createdandmaintainedbytheU.S.government’sTerroristScreeningCenter,ofpeoplewhoarenotpermittedtoboardacommercialaircraftfortravelinoroutoftheUnitedStates.
4、SupposeweneedtowriteaprogramthatcheckswhetherapersonisontheNo-Flylist.YoucanusealisttostorenamesintheNo-Flylist.However,amoreefficientdatastructureforthisapplicationisaset.SupposeyourprogramalsoneedstostoredetailedinformationaboutterroristsintheNo-Flylist.Thedetailedinform
5、ationsuchasgender,height,weight,andnationalitycanberetrievedusingthenameasthekey.Amapisanefficientdatastructureforsuchatask.4ReviewofJavaCollectionFrameworkhierarchySetandListaresubinterfacesofCollection.5TheCollectioninterfaceistherootinterfaceformanipulatingacollectionof
6、objects.6TheSetInterfaceTheSetinterfaceextendstheCollectioninterface.Itdoesnotintroducenewmethodsorconstants,butitstipulatesthataninstanceofSetcontainsnoduplicateelements.TheconcreteclassesthatimplementSetmustensurethatnoduplicateelementscanbeaddedtotheset.Thatisnotwoeleme
7、ntse1ande2canbeinthesetsuchthate1.equals(e2)istrue.7TheSetInterfaceHierarchy8TheAbstractSetClassTheAbstractSetclassisaconvenienceclassthatextendsAbstractCollectionandimplementsSet.TheAbstractSetclassprovidesconcreteimplementationsfortheequalsmethodandthehashCodemethod.Theh
8、ashcodeofasetisthesumofthehashcodeofalltheelementsintheset.Sincethesizemethodanditeratorm