Qhov sib txawv ntawm qhov sib npaug thiab hashCode hauv Java

Cov txheej txheem:

Qhov sib txawv ntawm qhov sib npaug thiab hashCode hauv Java
Qhov sib txawv ntawm qhov sib npaug thiab hashCode hauv Java

Video: Qhov sib txawv ntawm qhov sib npaug thiab hashCode hauv Java

Video: Qhov sib txawv ntawm qhov sib npaug thiab hashCode hauv Java
Video: Saib Mis paub Poj niam (hluas nkauj) tus yam ntxwv lub siab 2024, Hlis ntuj nqeg
Anonim

Qhov Tseem Ceeb Qhov txawv - sib npaug vs hashCode hauv Java

Qhov sib npaug zoo ib yam li tus neeg teb xov tooj==, uas yog sim rau cov khoom ntiag tug es tsis yog khoom vaj huam sib luag. Lub hashCode yog ib txoj hauv kev uas ib chav kawm implicitly lossis meej meej rhuav tshem cov ntaub ntawv khaws cia hauv ib qho piv txwv ntawm chav kawm rau hauv ib tus nqi hash, uas yog 32-ntsis kos npe tus lej. Qhov sib txawv tseem ceeb ntawm qhov sib npaug thiab hashCode hauv Java yog qhov sib npaug yog siv los sib piv ob yam khoom thaum lub hashCode siv hauv hashing los txiav txim seb pawg twg ib yam khoom yuav tsum tau categorized rau hauv.

Yuav ua li cas sib npaug hauv Java?

Txoj kev sib npaug yog siv los sib piv ob yam khoom. Lub neej ntawd sib npaug txoj kev yog txhais nyob rau hauv cov khoom chav kawm. Qhov kev siv ntawd zoo ib yam li tus neeg ua haujlwm==. Ob yam khoom siv sib npaug tsuas yog tias lawv taw tes rau tib yam khoom. Nws muaj peev xwm hla dhau txoj kev sib npaug.

Qhov sib txawv ntawm qhov sib npaug thiab hashCode hauv Java
Qhov sib txawv ntawm qhov sib npaug thiab hashCode hauv Java

Daim duab 01: Java Program nrog sib npaug

nqe lus System.out.println(s1.equals(s2)) yuav muab cov lus teb cuav vim s1 thiab s2 yog hais txog ob yam khoom sib txawv. Nws zoo ib yam li cov lus, System.out.println(s1==s2);

nqe lus System.out.println(s1.equals(s3)) yuav muab cov lus teb tseeb vim s1 thiab s3 yog hais txog tib yam khoom. Nws zoo ib yam li nqe lus, System.out.println(s1==s3);

Tsis muaj txoj hauv kev sib npaug hauv Cov Tub Kawm Ntawv. Yog li ntawd, qhov sib npaug hauv Object class hu ua. Muaj tseeb yog tshwm sim tsuas yog hais tias cov khoom siv yog taw tes rau tib yam khoom.

Qhov txawv ntawm qhov sib npaug thiab hashCode hauv Java_Figure 02
Qhov txawv ntawm qhov sib npaug thiab hashCode hauv Java_Figure 02

Daim duab 02: Java Program nrog Overridden sib npaug

Raws li cov kev pab cuam saum toj no, txoj kev sib npaug yog overridden. Ib yam khoom raug xa mus rau txoj kev, thiab nws yog ntaus ntawv pov rau Tub Kawm Ntawv. Tom qab ntawd, cov txiaj ntsig id raug kuaj xyuas. Yog tias tus nqi id zoo sib xws, nws yuav rov qab muaj tseeb. Yog tsis yog, nws yuav rov qab cuav. Cov ids ntawm s1 thiab s2 zoo ib yam. Yog li, nws yuav luam tawm tiag. Cov ids ntawm s1 thiab s3 kuj zoo sib xws, yog li nws yuav luam tawm tseeb.

HashCode hauv Java yog dab tsi?

Tus hashCode yog siv hauv hashing los txiav txim siab rau pawg twg cov khoom yuav tsum tau muab faib ua. Ib pawg ntawm cov khoom tuaj yeem sib koom tib yam hashCode. Kev hashing kom raug tuaj yeem faib cov khoom sib txawv mus rau ntau pawg.

A hashCode kom raug tuaj yeem muaj cov khoom raws li hauv qab no. Xav tias muaj ob yam khoom xws li obj1 thiab obj2. Yog tias obj1.equals(obj2) muaj tseeb, ces obj1.hashCode() yog sib npaug rau obj2.hashCode(). Yog tias obj1.equals(obj2) tsis tseeb, nws tsis tsim nyog tias obj1.hashCode() tsis sib npaug rau obj2.hashCode(). Ob yam khoom tsis sib xws kuj tseem yuav muaj tib lub hashCode.

Qhov txawv ntawm qhov sib npaug thiab hashCode hauv Java_Figure 03
Qhov txawv ntawm qhov sib npaug thiab hashCode hauv Java_Figure 03

Daim duab 03: Cov tub ntxhais kawm chav nrog qhov sib npaug thiab hashCode

Qhov txawv ntawm qhov sib npaug thiab hashCode hauv Java
Qhov txawv ntawm qhov sib npaug thiab hashCode hauv Java

Daim duab 04: Txoj Haujlwm Tseem Ceeb

Cov tub ntxhais kawm chav muaj qhov sib npaug thiab hashCode txoj kev. Txoj kev sib npaug hauv chav kawm Tub Ntxhais Kawm yuav tau txais ib qho khoom. Yog hais tias tus kwv yog null, nws yuav rov qab tsis tseeb. Yog tias cov chav kawm ntawm cov khoom tsis zoo ib yam, nws yuav rov qab tsis tseeb. Cov nqi id raug txheeb xyuas hauv ob qho khoom. Yog tias lawv zoo sib xws, nws yuav rov qab muaj tseeb. Tsis yog nws yuav rov dag.

Nyob rau hauv qhov kev zov me nyuam, cov khoom s1 thiab s2 raug tsim. Thaum hu s1.equals(s2) yuav muab qhov tseeb vim hais tias txoj kev sib npaug yog overridden thiab nws xyuas cov id qhov tseem ceeb ntawm ob yam khoom. Txawm hais tias lawv tau xa mus rau ob yam khoom, cov lus teb muaj tseeb vim tias tus nqi id ntawm s1 thiab s2 yog tib yam. Raws li s1.equals(s2) muaj tseeb, tus hashCode ntawm s1 thiab s2 yuav tsum sib npaug. Kev luam tawm hashCode ntawm s1 thiab s2 muab tus nqi tib yam. Txoj kev hashCode tuaj yeem siv nrog Sau xws li HashMap.

Qhov txawv ntawm qhov sib npaug thiab hashCode hauv Java yog dab tsi?

sib npaug vs hashCode hauv Java

sib npaug yog ib txoj hauv kev hauv Java uas ua zoo ib yam li tus neeg teb xov tooj==, uas yog sim rau cov khoom ntiag tug es tsis yog khoom vaj huam sib luag. hashCode yog ib txoj hauv kev uas cov chav kawm implicitly lossis meej meej rhuav tshem cov ntaub ntawv khaws cia hauv qhov piv txwv ntawm chav kawm rau hauv ib tus nqi hash.
Txoj kev sib npaug yog siv los sib piv ob yam khoom. Tus qauv yog siv hauv hashing los txiav txim siab seb pawg twg yuav tsum muab tso rau hauv.

Summary – sib npaug vs hashCode hauv Java

Qhov sib txawv ntawm qhov sib npaug thiab hashCode hauv Java yog qhov sib npaug yog siv los sib piv ob yam khoom thaum lub hashCode siv hauv hashing los txiav txim seb pawg twg ib yam khoom yuav tsum tau muab faib ua.

Pom zoo: