Qhov txawv ntawm TreeSet thiab TreeMap

Cov txheej txheem:

Qhov txawv ntawm TreeSet thiab TreeMap
Qhov txawv ntawm TreeSet thiab TreeMap

Video: Qhov txawv ntawm TreeSet thiab TreeMap

Video: Qhov txawv ntawm TreeSet thiab TreeMap
Video: Qhov Txawv Ntawm Lus Hmoob Thiab Lus Mab Mawb 2024, Kaum ib hlis
Anonim

Qhov Tseem Ceeb - TreeSet vs TreeMap

Ib qho array yog siv los khaws cov ntaub ntawv ntawm tib hom. Feem ntau cov lus programming txhawb Arrays. Txawm hais tias ib qho array tuaj yeem khaws ntau tus nqi; muaj qhov tsis zoo loj. Thaum cov array raug tsim, nws hloov tsis tau. Yog tias tus programmer tshaj tawm ib qho array ntawm 10 lub ntsiab lus, ces nws tsis tuaj yeem khaws 15 lub ntsiab lus. Thaum tus programmer tshaj tawm ib qho array ntawm 10 lub ntsiab lus thiab khw muag khoom tsuas yog 5 lub ntsiab lus, qhov seem ntawm lub cim xeeb tau muab pov tseg. Programming lus xws li Java muaj Collections khaws cov ntaub ntawv ntsiab lus dynamically. Muaj ib tug xov tooj ntawm collections. Cov ntawv sau pab ua ntxiv, tshem tawm cov ntsiab lus thiab lwm yam haujlwm. Lub hauv paus interface yog hu ua Collection. Teem, Sau thiab Queue yog qee qhov kev cuam tshuam uas txuas ntxiv rau Sau interface. Daim ntawv qhia yog ib qho kev sib txuas ntawm kev sau hierarchy, tab sis nws tsis txuas ntxiv qhov Collection interface. TreeSet yog ib chav kawm uas siv lub teeb interface thiab khaws cov ntsiab lus hauv kev nce qib. TreeMap yog ib chav kawm uas siv Daim Ntawv Qhia interface thiab khaws cov yuam sij, tus nqi sib tw hauv kev nce qib. Qhov ntawd yog qhov sib txawv tseem ceeb. Kab lus no tham txog qhov txawv ntawm TreeSet thiab TreeMap.

TreeSet yog dab tsi?

The TreeSet yog chav kawm uas siv teeb tsa interface. TreeSet tuav cov khoom tshwj xeeb. TreeSet siv NavigableSet interface. Lub Navigable interface txuas ntxiv SortedSet, Teem, Sau thiab Iterable interfaces hauv kev txiav txim siab hierarchical. TreeSet khaws cov khoom hauv qhov kev txiav txim nce mus. Yog tias qhov kev txiav txim ntawm qhov ntxig yog A, C, B, TreeSet yuav khaws lawv li A, B, C. Muaj cov txheej txheem ntawm TreeSet. Txoj kev ntxiv yog siv los ntxiv ib lub caij rau lub Teeb. Txoj kev tshem tawm yog siv los tshem tawm cov ntsiab lus. Txoj kev ntshiab yog siv los tshem tawm tag nrho cov ntsiab lus. Cov txheej txheem muaj cov txheej txheem rov qab qhov tseeb yog tias cov ntsiab lus tau teev tseg nyob rau hauv Teeb. Lawv yog qee txoj hauv kev muab los ntawm TreeSet. Xa mus rau qhov program hauv qab no.

Qhov txawv ntawm TreeSet thiab TreeMap
Qhov txawv ntawm TreeSet thiab TreeMap

Daim duab 01: Program siv TreeSet

Raws li cov kev pab cuam saum toj no, Treeset yog ib yam khoom ntawm TreeSet. Nws tuaj yeem khaws cov hlua. Cov ntsiab lus ntxiv yog siv txoj kev ntxiv. Qhov kev txiav txim ntxig yog A, C, D thiab B. Siv lub iterator, cov nqi khaws cia tau luam tawm rau qhov screen. Cov ntsiab lus tau muab khaws cia rau hauv qhov kev txiav txim A, B, C, D. Yog li ntawd, TreeSet tswj kev nce qib ntawm cov ntsiab lus ntawm Cov Txheej Txheem. Yog tias muaj lwm lub caij ua "D" nws yuav tsis luam tawm vim tias lub caij D twb muaj nyob rau hauv Cov Txheej Txheem. Nws ib txwm khaws cov khoom tshwj xeeb.

TreeMap yog dab tsi?

TreeMap yog chav kawm uas siv Daim Ntawv Qhia interface. Daim ntawv qhia txhawb nqa tus nqi tseem ceeb. Txhua tus yuam sij, tus nqi khub yog qhov nkag. Txhua tus yuam sij yog qhov tshwj xeeb thiab muaj tus nqi sib xws. Txoj kev muajKey siv los nrhiav tus yuam sij tshwj xeeb thaum muaj txoj hauv kev siv los nrhiav tus nqi tshwj xeeb. Txoj kev tau txais yog siv los nrhiav tus nqi sib xws rau tus yuam sij muab. Txoj kev muab tso yog siv los khaws tus nqi nrog tus yuam sij muab. Nws tseem tuaj yeem tshem tawm ib qho ntawm ib qho tseem ceeb uas siv txoj kev tshem tawm. Nov yog qee txoj hauv kev ntawm Map interface. Nws pab tshawb nrhiav, ntxig thiab tshem tawm cov ntsiab lus raws li tus yuam sij. TreeMap chav kawm siv NavigableMap. NavigableMap txuas ntxiv SortedMap. SortedMap txuas daim ntawv qhia. Yog li, cov txheej txheem ntawm Daim Ntawv Qhia tuaj yeem siv nrog TreeMap. Xa mus rau qhov program hauv qab no.

Qhov txawv tseem ceeb ntawm TreeSet thiab TreeMap
Qhov txawv tseem ceeb ntawm TreeSet thiab TreeMap

Daim duab 02: Txoj Haujlwm siv TreeMap

Raws li cov kev pab cuam saum toj no, ib qho khoom ntawm TreeMap yog tsim. Tus programmer tuaj yeem ntxiv cov ntsiab lus siv cov khoom. Txoj kev muab tso yog siv los ntxig qhov tseem ceeb, tus nqi khub. Txoj kev tau txais yog siv nrog tus yuam sij tshwj xeeb los nqa cov ntsiab lus. Tus programmer siv tau Map. Entry los luam tag nrho cov yuam sij thiab qhov tseem ceeb. Thaum soj ntsuam cov zis, nws tsis tswj qhov kev txiav txim tso. Nws khaws cov khoom nyob rau hauv qhov kev txiav txim nce mus.

Qhov Zoo sib xws ntawm TreeSet thiab TreeMap yog dab tsi?

  • Ob qho tib si TreeSet thiab TreeMap yog nyob rau hauv kev sau hierarchy.
  • Ob leeg TreeSet thiab TreeMap tuav qhov kev txiav txim nce mus.
  • Ob qho tib si TreeSet thiab TreeMap tuaj yeem khaws thiab tswj tau ntau yam.

Qhov txawv ntawm TreeSet thiab TreeMap yog dab tsi?

TreeSet vs TreeMap

TreeSet yog ib chav kawm uas siv lub teeb interface thiab khaws cov khoom hauv kev nce qib. TreeMap yog ib chav kawm uas siv Daim Ntawv Qhia interface thiab khaws cov yuam sij, tus nqi sib tw hauv kev nce qib.
Ua tiav Interface
TreeSet siv Teeb interface. TreeMap siv Daim Ntawv Qhia interface.

Summary – TreeSet vs TreeMap

Ib qho array yog siv los khaws cov ntsiab lus, tab sis nws tsis pab khaws cov ntsiab lus dynamically. Cov lus programming xws li Java muaj Cov Sau los khaws cov ntaub ntawv dynamically. Collection yog lub hauv paus chav kawm nyob rau hauv sau hierarchy. Nws muaj cov chav kawm thiab interfaces los ua haujlwm xws li ntxiv, tshem tawm cov ntsiab lus. Teeb thiab Daim Ntawv Qhia yog ob qhov sib cuam tshuam ntawm Sau hierarchy. TreeSet yog ib chav kawm uas siv lub teeb interface thiab khaws cov ntsiab lus hauv kev nce qib. TreeMap yog ib chav kawm uas siv Daim Ntawv Qhia interface thiab khaws cov yuam sij, tus nqi khub hauv kev nce qib. Qhov ntawd yog qhov txawv ntawm TreeSet thiab TreeMap.

Pom zoo: