Qhov txawv ntawm HashMap thiab TreeMap

Cov txheej txheem:

Qhov txawv ntawm HashMap thiab TreeMap
Qhov txawv ntawm HashMap thiab TreeMap

Video: Qhov txawv ntawm HashMap thiab TreeMap

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

Qhov Tseem Ceeb - HashMap vs TreeMap

Hauv kev ua haujlwm, muaj ntau yam txheej txheem los sau cov ntaub ntawv. Kev sau yog ib txoj hauv kev khaws cov ntaub ntawv. Programming lus xws li Java siv Collections. Nws yog lub moj khaum nrog cov chav kawm thiab interfaces rau khaws cia thiab tswj cov txheej txheem ntawm cov ntaub ntawv. Nyob rau hauv ib txwm array, muaj ib tug taag naj npawb ntawm cov khoom khaws cia. Qhov ntawd yog qhov txwv ntawm arrays. Hloov chaw, tus programmer tuaj yeem siv cov ntawv sau. Kev ua haujlwm xws li ntxig, tshem tawm, txheeb xyuas thiab tshawb nrhiav tuaj yeem ua tau siv cov khoom sau. Nyob rau hauv Java, daim ntawv qhia interface belongs rau collections. Daim ntawv qhia yog siv los sawv cev cov ntaub ntawv nyob rau hauv tus yuam sij, tus nqi khub. Tsuas muaj cov yuam sij tshwj xeeb, thiab txhua tus muaj tus nqi sib xws. HashMap thiab TreeMap yog cov chav kawm uas siv Daim Ntawv Qhia interface. HashMap yog daim ntawv qhia raws li cov chav kawm sau uas yog siv los khaws cov khoom tseem ceeb thiab tus nqi uas tsis tuav ib qho kev txiav txim hauv cov ntaub ntawv. TreeMap yog daim ntawv qhia raws li chav kawm sau uas yog siv los khaws cov ntsiab lus tseem ceeb thiab tus nqi uas tswj hwm qhov kev txiav txim nce qib ntawm cov ntaub ntawv. Qhov sib txawv tseem ceeb ntawm HashMap thiab TreeMap yog tias HashMap tsis tswj hwm qhov kev txiav txim tshwj xeeb hauv cov ntaub ntawv thaum TreeMap tswj hwm qhov nce qib ntawm cov ntaub ntawv ntsiab lus.

HashMap yog dab tsi?

HashMap yog chav kawm uas siv daim ntawv qhia kev sib tham. Nws txuas rau AbstractMap chav kawm thiab siv Daim Ntawv Qhia interface. Ib HashMap muaj qhov tseem ceeb, tus nqi khub. Txhua lub ntsiab yog tshwj xeeb. Nws yooj yim los nrhiav cov ntsiab lus hauv HashMap siv tus yuam sij. Tshaj tawm HashMap yog raws li hauv qab no.

public class HashMap txuas ntxiv AbstractMap siv Daim Ntawv Qhia, Cloneable, Serializable

K hais txog tus yuam sij thaum V hais txog tus nqi sib raug rau tus yuam sij tshwj xeeb ntawd. Txhua tus yuam sij, tus nqi khub yog qhov nkag ntawm HashMap.

Qhov txawv ntawm HashMap thiab TreeMap
Qhov txawv ntawm HashMap thiab TreeMap

Daim duab 01: Daim duab qhia chaw

Xaiv qhov xwm txheej raws li hauv qab no kom nkag siab HaspMap. Yog tias muaj tus programmer xav khaws cov npe ntawm cov tub ntxhais kawm ntawv thiab cov lej sib raug zoo, nws tuaj yeem siv HashMap. Cov npe menyuam kawm ntawv raug siv los nrhiav cov lej ntsuas. Yog li ntawd, cov tub ntxhais kawm cov npe yog cov yuam sij thaum cov lej ntsuas yog qhov tseem ceeb.

Qhov txawv ntawm HashMap thiab TreeMap _Daim duab 02
Qhov txawv ntawm HashMap thiab TreeMap _Daim duab 02

Daim duab 02: HashMap Program Siv Java

Raws li cov kev pab cuam saum toj no, ib qho khoom ntawm HashMap yog tsim. Tom qab ntawd tus programmer tuaj yeem ntxiv cov ntsiab lus siv cov khoom ntawd. Cov nqi tuaj yeem muab tso nrog txoj kev muab tso. Txhawm rau nqa cov txiaj ntsig, tus programmer yuav tsum siv txoj kev tau txais nrog tus yuam sij. Thaum siv studentList.get(“150”); nws yuav luam lub npe coj mus rau qhov ntsuas ntawd uas yog Ann. Yog tias tus programmer xav kom tau txais tag nrho cov txiaj ntsig, ces nws tuaj yeem siv Map. Entry los luam tag nrho cov yuam sij thiab qhov tseem ceeb. Thaum soj ntsuam cov zis, nws tuaj yeem pom tias HashMap tsis tuav ib qho kev txiav txim tshwj xeeb. Nws tsis sau cov ntsiab lus hauv qhov kev txiav txim tso. Cov ntsiab lus tau luam tawm hauv qhov kev txiav txim random.

TreeMap yog dab tsi?

Lub TreeMap yog chav kawm hauv Java uas siv Daim Ntawv Qhia interface. Zoo ib yam li HashMap, nws kuj siv rau khaws cov yuam sij, tus nqi khub tab sis nyob rau hauv qhov kev txiav txim nce. TreeMap siv cov NavigableMap thiab NavigableMap txuas ntxiv SortedMap thiab SortedMap txuas Daim Ntawv Qhia. Txhua lub ntsiab yog tshwj xeeb. Tshaj tawm TreeMap yog raws li hauv qab no.

pej xeem chav TreeMap txuas ntxiv AbstractMap siv NavigableMap, Cloneable, Serializable

K hais txog tus yuam sij thaum V hais txog tus nqi sib raug rau tus yuam sij tshwj xeeb ntawd. Txhua tus yuam sij, tus nqi khub yog qhov nkag ntawm TreeMap.

Qhov txawv ntawm HashMap thiab TreeMap
Qhov txawv ntawm HashMap thiab TreeMap

Daim duab 03: TreeMap Program siv Java

Raws li cov kev pab cuam saum toj no, ib qho khoom ntawm TreeMap yog tsim. Tom qab ntawd tus programmer tuaj yeem ntxiv cov ntsiab lus siv cov khoom ntawd. Cov txiaj ntsig tuaj yeem muab tso nrog txoj kev muab tso. Txhawm rau nqa cov txiaj ntsig, tus programmer yuav tsum siv txoj kev tau txais nrog tus yuam sij. Thaum siv studentList.get(“150”); nws yuav luam lub npe coj mus rau qhov ntsuas ntawd uas yog Ann. Yog tias tus programmer xav kom tau txais tag nrho cov txiaj ntsig, ces nws tuaj yeem siv Map. Entry los luam tag nrho cov yuam sij thiab qhov tseem ceeb. Thaum soj ntsuam cov zis, nws tuaj yeem pom tias TreeMap tswj hwm qhov kev txiav txim tshwj xeeb. Cov ntsiab lus tau luam tawm nyob rau hauv qhov kev txiav txim nce mus.

Dab tsi zoo sib xws ntawm HashMap thiab TreeMap?

  • Ob HashMap thiab TreeMap siv Daim Ntawv Qhia interface.
  • Ob HashMap thiab TreeMap tuaj yeem khaws thiab tswj ntau yam.
  • Ob HashMap thiab TreeMap muaj qhov tseem ceeb, tus nqi khub.
  • Ob HashMap thiab TreeMap tuaj yeem muaj ntau yam tsis muaj nuj nqis.
  • Tsis muaj kev txwv rau cov khoom uas tuaj yeem khaws cia hauv HashMap thiab TreeMap.

Qhov txawv ntawm HashMap thiab TreeMap yog dab tsi?

HashMap vs TreeMap

A HashMap yog Daim Ntawv Qhia raws li cov chav kawm sau uas yog siv los khaws cov yuam sij thiab tus nqi uas tsis tuav ib qho kev txiav txim tshwj xeeb hauv cov ntaub ntawv. A TreeMap yog daim ntawv qhia raws li cov chav kawm sau uas yog siv los khaws cov ntsiab lus tseem ceeb thiab tus nqi uas tswj hwm qhov nce qib ntawm cov ntaub ntawv ntsiab lus.
Order
The HashMap tsis tuav qhov kev txiav txim. Lub TreeMap tswj kev nce qib.
Null Key
Tus HashMap tuaj yeem muaj ib tus yuam sij null. Lub TreeMap tsis tuaj yeem muaj tus yuam sij null.
HashMap sai dua TreeMap. TreeMap qeeb dua HashMap.

Summary – HashMap vs TreeMap

Cov lus programming xws li Java muaj cov txheej txheem sau. Hauv arrays, tuaj yeem muaj tus lej tas li ntawm cov ntsiab lus. Yog li ntawd, qhov loj ntawm array yuav tsum tau pib thaum pib. Hauv kev sau, tus programmer tuaj yeem khaws ntau yam khoom raws li qhov xav tau. Tsis muaj qhov tshwj xeeb khaws cia. Daim ntawv qhia yog ib qho interface belongs rau sau lub moj khaum. HashMap yog daim ntawv qhia raws li cov chav kawm sau uas yog siv los khaws cov khoom tseem ceeb thiab tus nqi uas tsis tuav ib qho kev txiav txim tshwj xeeb hauv cov ntaub ntawv. TreeMap yog daim ntawv qhia raws li cov chav kawm sau uas yog siv los khaws cov ntsiab lus tseem ceeb thiab tus nqi uas tswj hwm qhov nce qib ntawm cov ntaub ntawv ntsiab lus. Kab lus no tau tham txog qhov sib txawv ntawm HashMap thiab TreeMap uas siv Daim Ntawv Qhia interface. Qhov txawv ntawm HashMap thiab TreeMap yog tias HashMap tsis tuav ib qho kev txiav txim tshwj xeeb hauv cov ntaub ntawv thaum TreeMap tswj qhov kev txiav txim nce qib ntawm cov ntaub ntawv.

Pom zoo: