Qhov txawv ntawm TreeSet thiab HashSet

Cov txheej txheem:

Qhov txawv ntawm TreeSet thiab HashSet
Qhov txawv ntawm TreeSet thiab HashSet

Video: Qhov txawv ntawm TreeSet thiab HashSet

Video: Qhov txawv ntawm TreeSet thiab HashSet
Video: Qhov Txawv Ntawm Lus Hmoob Thiab Lus Mab Mawb 2024, Hlis ntuj nqeg
Anonim

Qhov Tseem Ceeb Qhov txawv - TreeSet vs HashSet

Cov lus programming feem ntau txhawb Arrays. Nws yog cov qauv ntaub ntawv uas siv los khaws ntau lub ntsiab ntawm tib hom ntaub ntawv. Yog tias muaj array tshaj tawm rau rau lub ntsiab lus, ces nws tsis tuaj yeem siv los khaws kaum lub ntsiab lus. Yog li ntawd, arrays tsis dynamic thiab tsis tuaj yeem hloov qhov loj ntawm array thaum nws tau tshaj tawm. Programming lus xws li Java txhawb nqa Collections uas yog siv los khaws cov ntaub ntawv dynamically. Cov khoom siv txhawb nqa kev ua haujlwm xws li ntxiv cov ntsiab lus thiab tshem tawm cov ntsiab lus. Muaj ib tug xov tooj ntawm interfaces thiab chav kawm nyob rau hauv lub sau hierarchy. Lub hauv paus interface yog Collection interface. Teeb yog ib qho interface uas txuas ntxiv qhov Collection interface. Nws tsis pub luam tawm. TreeSet thiab HashSet yog ob chav kawm nyob rau hauv Collection hierarchy thiab ob qho tib si siv lub teeb interface. TreeSet yog ib chav kawm uas siv lub teeb interface thiab nws yog siv los khaws cov khoom tshwj xeeb hauv kev nce qib. HashSet yog ib chav kawm uas siv lub teeb interface thiab nws yog siv los khaws cov khoom tshwj xeeb uas siv Hashing mechanism. Qhov sib txawv tseem ceeb ntawm TreeSet thiab HashSet yog tias TreeSet khaws cov khoom nyob rau hauv qhov kev txiav txim nce mus ntxiv thaum HashSet tsis khaws cov khoom hauv qhov kev txiav txim nce mus. TreeSet thiab HashSet tsuas khaws cov khoom tshwj xeeb xwb.

TreeSet yog dab tsi?

TreeSet chav kawm siv NavigableSet interface. Lub NavigableSet interface txuas ntxiv SortedSet, Teeb, Sau thiab Iterable interfaces hauv kev txiav txim siab. TreeSet ib txwm tswj hwm qhov kev txiav txim nce. Yog hais tias cov ntsiab lus tau muab tso rau hauv B, A, C txiav txim, lawv yuav muab khaws cia li A, B, C. Cov txheej txheem xws li ntxiv (), tshem tawm () tuaj yeem siv nrog TreeSet khoom. Txoj kev ntxiv tuaj yeem siv los ntxiv cov khoom. Txoj kev tshem tawm yog siv los tshem tawm cov khoom los ntawm kev sau. Cov no yog qee txoj hauv kev uas tuaj yeem siv nrog TreeSet.

Qhov txawv ntawm TreeSet thiab HashSet
Qhov txawv ntawm TreeSet thiab HashSet

Daim duab 01: Program nrog TreeSet

Raws li cov kev pab cuam saum toj no, ib qho khoom ntawm hom TreeSet yog tsim. Cov hlua cov ntaub ntawv ntsiab lus ntxiv rau qhov khoom siv txoj kev ntxiv. Cov ntaub ntawv tso rau hauv qhov kev txiav txim yog A, D, A, B, C, D. Siv cov iterator, cov nqi khaws cia yog luam tawm rau qhov screen. Cov zis yog A, B, C, D. Txawm hais tias, muaj ob tsab ntawv A thiab ob tsab ntawv D, cov zis tso tawm ib qho A thiab ib D txhua. Yog li ntawd, TreeSet khaws cov khoom tshwj xeeb. Tsis muaj kev txiav txim siab tshwj xeeb tab sis thaum soj ntsuam cov zis, nws tuaj yeem pom tias TreeSet tswj hwm qhov kev txiav txim nce qib ntawm cov khoom.

HashSet yog dab tsi?

HashSet chav kawm txuas ntxiv chav kawm AbstractSet uas siv Teeb Interface. Lub teeb interface tau txais kev sau thiab Iterable interfaces hauv kev txiav txim siab hierarchical. Hauv HashSet, tsis muaj kev lav phib xaub tias cov ntsiab lus yuav tswj hwm qhov kev txiav txim nce mus thiab qhov kev txiav txim tso. Yog tias qhov kev txiav txim ntxig yog A, B, C ces qhov tseem ceeb yuav khaws cia li C, A, B. Kev khaws cia kuj tuaj yeem yog A, B, C tab sis tsis muaj kev lees paub tias qhov kev txiav txim tso lossis nce qib tau khaws cia.

Qhov txawv tseem ceeb ntawm TreeSet thiab HashSet
Qhov txawv tseem ceeb ntawm TreeSet thiab HashSet

Daim duab 02: Program nrog HashSet

Raws li cov kev pab cuam saum toj no, ib qho khoom ntawm hom HashSet yog tsim. Cov hlua cov ntaub ntawv ntsiab lus ntxiv rau qhov khoom siv txoj kev ntxiv. Cov ntaub ntawv tso rau hauv kev txiav txim yog L, R, M, M, R, L. Siv cov iterator, cov nqi khaws cia yog luam tawm rau qhov screen. Cov zis yog R L M. Txawm hais tias, muaj ob tsab ntawv L, R thiab M ntawm txhua tus, tsuas yog ib tsab ntawv los ntawm txhua qhov tso tawm. Yog li, HashSet khaws cov khoom tshwj xeeb. Thaum soj ntsuam cov zis, nws pom tau tias tsis muaj qhov nce mus los lossis qhov kev txiav txim tso tseg tau khaws cia.

Dab tsi yog qhov zoo sib xws ntawm TreeSet thiab HashSet?

  • Ob qho tib si TreeSet thiab HashSet yog cov chav kawm muaj nyob rau hauv kev sau hierarchy.
  • Ob qho tib si TreeSet thiab HashSet khw muag khoom nkaus xwb.
  • Ob leeg TreeSet thiab HashSet tuaj yeem siv los khaws thiab tswj ntau yam.
  • Ob Lub TreeSet thiab HashSet tsis tuav qhov kev txiav txim tso.

Qhov txawv ntawm TreeSet thiab HashSet yog dab tsi?

TreeSet vs HashSet

TreeSet yog ib chav kawm nyob rau hauv kev sau hierarchy uas yog siv los khaws cov khoom tshwj xeeb hauv kev nce qib. HashSet yog chav kawm hauv kev sau hierarchy uas yog siv los khaws cov khoom tshwj xeeb uas siv Hashing mechanism.
Khoom khaws cia
TreeSet khaws cov khoom hauv kev nce qib. HashSet tsis khaws cov khoom hauv qhov kev txiav txim nce mus.

Summary – TreeSet vs HashSet

Hauv kev ua haujlwm, nws yuav tsum khaws cov ntaub ntawv cov ntsiab lus dynamically. Programming lus xws li Java txhawb nqa Collections kom ua tiav txoj haujlwm no. Muaj ib tug xov tooj ntawm interfaces thiab chav kawm nyob rau hauv lub sau hierarchy. TreeSet thiab HashSet yog ob chav kawm nyob rau hauv Collection hierarchy. Ob leeg siv lub teeb interface. TreeSet yog chav kawm uas siv lub teeb interface thiab nws yog siv los khaws cov khoom tshwj xeeb hauv kev nce qib. HashSet yog ib chav kawm uas siv lub teeb interface thiab nws yog siv los khaws cov khoom tshwj xeeb uas siv Hashing mechanism. Qhov sib txawv ntawm TreeSet thiab HashSet yog tias TreeSet khaws cov khoom nyob rau hauv qhov kev txiav txim nce mus thaum HashSet tsis khaws cov khoom hauv qhov kev txiav txim nce mus. Kab lus no tau tham txog qhov sib txawv ntawm TreeSet thiab HashSet.

Pom zoo: