Vim li cas peb thiaj siv TreeMap hauv Java?
Vim li cas peb thiaj siv TreeMap hauv Java?

Video: Vim li cas peb thiaj siv TreeMap hauv Java?

Video: Vim li cas peb thiaj siv TreeMap hauv Java?
Video: 🥰💯 vim li cas peb thiaj yuav tau siv lotion tiv thaiv tus kheej ❤️❤️ 2024, Kaum ib hlis
Anonim

Cov TreeMap hauv Java yog siv los siv Daim Ntawv Qhia interface thiab NavigableMap nrog rau Chav Kawm Abstract. Daim ntawv qhia yog txheeb raws li qhov kev txiav txim ntawm nws cov yuam sij, lossis los ntawm tus Sib piv muab rau lub sijhawm tsim daim ntawv qhia, nyob ntawm seb tus tsim qauv twg yog siv.

Cov lus nug tseem yog, TreeMap ua haujlwm li cas?

TreeMap hauv Java. Cov TreeMap yog siv los siv Map interface thiab NavigableMap nrog rau Chav Kawm Abstract. Tsis tas li ntawd, tag nrho nws cov khoom khaws cia hauv lub TreeMap raug txheeb los ntawm qhov tseem ceeb. TreeMap ua sorting nyob rau hauv tej yam ntuj tso kev txiav txim ntawm nws tus yuam sij, nws kuj tso cai rau koj siv Comparator rau kev cai sorting siv.

Ib yam li ntawd, vim li cas HashMap sai dua TreeMap? Nws muab kev ua haujlwm ntawm O (1), thaum TreeMap muab kev ua haujlwm ntawm O(log(n)) ntxiv, tshawb nrhiav, thiab tshem tawm cov khoom. Vim li no, HashMap feem ntau yog sai dua . A TreeMap siv txoj kev nco zoo dua yog li nws yog qhov kev siv Daim Ntawv Qhia zoo rau koj yog tias koj tsis paub meej txog cov ntsiab lus kom muaj nuj nqis uas yuav tsum tau khaws cia hauv lub cim xeeb.

Tib neeg kuj nug, TreeMap puas siv hashing?

TreeMap yog qeeb hauv kev sib piv rau HashMap vim nws muab kev ua haujlwm ntawm O(log(n)) rau feem ntau cov haujlwm xws li add(), tshem tawm() thiab muaj(). HashMap chav kawm siv tus hash rooj. TreeMap sab hauv siv ib tsob ntoo Liab-Dub, uas yog tus kheej ntsuas Binary Nrhiav tsob ntoo. Txoj kev sib npaug () ntawm daim ntawv qhia chav kawm overrides nws.

Puas yog TreeMap txheeb tau?

Cov ntaub ntawv hauv a TreeMap yog ib txwm txheeb raws li kev txiav txim ntawm ntuj tsim cov yuam sij, lossis raws li kev cai sib piv uas koj tuaj yeem muab tau thaum lub sijhawm tsim cov TreeMap . TreeMap tsis tuaj yeem muaj tus yuam sij null. Txawm li cas los xij, Nws tuaj yeem muaj nuj nqis null. TreeMap tsis synchronized.

Pom zoo: