Cov txheej txheem:

Peb puas tuaj yeem sib piv ob daim duab qhia hauv Java?
Peb puas tuaj yeem sib piv ob daim duab qhia hauv Java?

Video: Peb puas tuaj yeem sib piv ob daim duab qhia hauv Java?

Video: Peb puas tuaj yeem sib piv ob daim duab qhia hauv Java?
Video: Peb mus saib Dej nag vaj ua moviev sib aim 2024, Kaum ib hlis
Anonim

Los ntawm lub neej ntawd, HashMap . equals() txoj kev sib piv ob hashmap los ntawm tus nqi tseem ceeb. Nws txhais tau tias ob leeg hmoob piv txwv yuav tsum muaj ib yam tseem ceeb-tus nqi khub thiab ob qho tib si yuav tsum muaj qhov loj me. Qhov kev txiav txim ntawm tus nqi tseem ceeb ua tau yuav txawv thiab ua tsis ua si hauv lub luag haujlwm kev sib piv.

Ntawm no, koj ua li cas piv ob qhov tseem ceeb ntawm daim ntawv qhia?

Txoj hauv kev kom yog los sib piv cov duab qhia txog tus nqi sib npaug yog:

  1. Txheeb xyuas tias daim duab qhia chaw yog tib qhov loj (!)
  2. Tau cov yuam sij ntawm ib daim ntawv qhia.
  3. Rau txhua tus yuam sij los ntawm qhov teeb tsa koj tau rov qab, xyuas tias tus nqi muab los ntawm txhua daim ntawv qhia rau tus yuam sij ntawd yog tib yam (yog tias tus yuam sij tsis tuaj ntawm ib daim ntawv qhia, qhov ntawd yog tag nrho tsis ua tiav ntawm kev sib npaug)

Ib yam li ntawd, kuv yuav piv ob lub npe hauv Java li cas? Koj ua tau piv ob array cov npe siv cov equals() method of the ArrayList chav kawm, txoj kev no lees txais a lis khoom raws li ib qho parameter, piv nws nrog cov khoom tam sim no, nyob rau hauv cov ntaub ntawv ntawm qhov sib tw nws rov qab muaj tseeb thiab yog tias tsis yog nws rov qab tsis tseeb.

Hais txog qhov no, koj ua li cas piv ob daim ntawv qhia hash?

Rau Sib piv Hashmaps hauv java, mas ob txoj kev siv xws li hashCode() thiab equals(). Yog hashCode ntawm ob daim ntawv qhia yog sib npaug ces peb tuaj yeem mus rau qhov equals() method, as hashCode of ob HashMaps tuaj yeem sib npaug tab sis tsis muaj tseeb hais tias lawv sib npaug thiab.

Peb puas tuaj yeem sib piv ob daim duab qhia hauv C ++?

Cov daim ntawv qhia ::key_comp() yog ib qho haujlwm hauv STL hauv C++ uas xa rov qab ib daim qauv ntawm kev sib piv khoom siv los ntawm lub thawv uas piv yuam sij. Rov qab tus nqi: Txoj kev no rov qab cov kev sib piv khoom siv los ntawm lub thawv uas piv yuam sij. // C++ program los ua qauv qhia daim ntawv qhia ::key_comp().

Pom zoo: