Grokking The Java Developer Interview: More Than 200 Questions To Crack The Java, Spring, SpringBoot & Hibernate Interview



Download 7,31 Mb.
Pdf ko'rish
bet80/89
Sana21.04.2022
Hajmi7,31 Mb.
#571320
1   ...   76   77   78   79   80   81   82   83   ...   89
Bog'liq
Grokking-The-Java-Developer-Interview-More-Than-200-Questions-To-Crack-The-Java -Spring -SpringBoot-

Scenario 2: when only equals() method is implemented by
Employee class


Now, can you predict the output of our TestHashMap class?


Let’s see the output:
Well, nothing’s changed here. Because even though e1 and e2 are
equal according to our newly implemented equals() method, they still
have different hashCode as the Object’s class hashCode() is used.
So the equals and hashCode contract is not followed and both e1,
e2 got inserted in HashMap.
Scenario 3: when only hashCode() method is implemented:


Let’s run our TestHashMap class again and see the output:


Well, now we have same hashCode for e1 and e2, but Object’s
equals method still checks the references and as references are
different, both are not equal and are inserted in the hashmap.
Scenario 4: When both equals and hashCode are implemented
properly:


Output:


Here, both e1 and e2 are equals as we are comparing the contents
of them in our equals() method, so their hashCodes must be same,
which they are. So value of e1 which was 100 got replaced by 200,
and size of hashmap is 2.
You can be asked to write the equals() and hashCode() methods
implementation by hand also, so you should pay attention to how
these are implemented.
Question 106: How to make a HashMap
synchronized?
Answer: Collections.synchronizedMap(map);



Download 7,31 Mb.

Do'stlaringiz bilan baham:
1   ...   76   77   78   79   80   81   82   83   ...   89




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish