• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 19:30
CEST 01:30
KST 08:30
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL20] Ro24 Preview Pt2: Take-Off7[ASL20] Ro24 Preview Pt1: Runway132v2 & SC: Evo Complete: Weekend Double Feature4Team Liquid Map Contest #21 - Presented by Monster Energy9uThermal's 2v2 Tour: $15,000 Main Event18
Community News
Weekly Cups (Aug 18-24): herO dethrones MaxPax6Maestros of The Game—$20k event w/ live finals in Paris34Weekly Cups (Aug 11-17): MaxPax triples again!13Weekly Cups (Aug 4-10): MaxPax wins a triple6SC2's Safe House 2 - October 18 & 195
StarCraft 2
General
BoxeR's Wings Episode 2 - Fan Translation Greatest Players of All Time: 2025 Update #1: Maru - Greatest Players of All Time A Eulogy for the Six Pool Geoff 'iNcontroL' Robinson has passed away
Tourneys
$5,000 WardiTV Summer Championship 2025 Maestros of The Game—$20k event w/ live finals in Paris $5,100+ SEL Season 2 Championship (SC: Evo) Esports World Cup 2025 Sparkling Tuna Cup - Weekly Open Tournament
Strategy
Custom Maps
External Content
Mutation # 488 What Goes Around Mutation # 487 Think Fast Mutation # 486 Watch the Skies Mutation # 485 Death from Below
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ Post ASL20 Ro24 discussion. No Rain in ASL20? How do I speak directly to Coinbase?1-(888)-419-97 Recent recommended BW games
Tourneys
[ASL20] Ro24 Group D [ASL20] Ro24 Group E Small VOD Thread 2.0 [Megathread] Daily Proleagues
Strategy
Muta micro map competition Simple Questions, Simple Answers Fighting Spirit mining rates [G] Mineral Boosting
Other Games
General Games
Stormgate/Frost Giant Megathread General RTS Discussion Thread Nintendo Switch Thread Dawn of War IV Path of Exile
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread The year 2050 European Politico-economics QA Mega-thread
Fan Clubs
INnoVation Fan Club SKT1 Classic Fan Club!
Media & Entertainment
Anime Discussion Thread Movie Discussion! [Manga] One Piece [\m/] Heavy Metal Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread High temperatures on bridge(s) Gtx660 graphics card replacement
TL Community
The Automated Ban List TeamLiquid Team Shirt On Sale
Blogs
How Culture and Conflict Imp…
TrAiDoS
RTS Design in Hypercoven
a11
Evil Gacha Games and the…
ffswowsucks
INDEPENDIENTE LA CTM
XenOsky
[Girl blog} My fema…
artosisisthebest
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1034 users

Java Question...

Blogs > blagoonga123
Post a Reply
blagoonga123
Profile Blog Joined July 2007
United States2068 Posts
Last Edited: 2008-04-10 21:19:47
April 10 2008 21:19 GMT
#1
The equals method in the Measurement class was originally defined something like this:

public boolean equals (Measurement m) {
return m.myFeet == this.myFeet && m.myInches == this.myInches;
}

With Measurement.equals defined in that way, but with a correctly defined hashCode method, what is the contents of the hash table after execution of the following program segment?

HashMap table = new HashMap ( );
table.put (new Measurement (6, 2), "clancy"); // Mike Clancy is 6'2"
table.put (new Measurement (6, 2), "hale"); // so is Paul Hale


Basically I'm unsure of whether or not this actually changes anything. I think there might be an error, because when it checks the bucket, it will check if the new measurement .equals an object, but if not then it should work fine right?

****
FOOL! Pain is my friend! Now let me introduce you to it!
haduken
Profile Blog Joined April 2003
Australia8267 Posts
April 10 2008 22:27 GMT
#2
"it will check if the new measurement .equals an object, but if not then it should work fine right?"

not sure what you mean there. but assuming that the rest of the code is correct. then when collision happens (when 1 measurement have more than 1 object associated with it.), then code should handle that (which what a hashtable should do). So if you code doesn't do that then you will have problems but for the above question then yes it will work so long nothing equals.
Rillanon.au
haduken
Profile Blog Joined April 2003
Australia8267 Posts
April 10 2008 22:32 GMT
#3
actually, it depends on the definition of thje HashMap class. If it handles collision for you (which i'm assuming it will since this is java ~_~) then what is the point of the equals method when all you want is to put them on a hash table.
Rillanon.au
r3dox
Profile Blog Joined May 2003
Germany261 Posts
April 10 2008 22:43 GMT
#4
doesnt it have to be public boolean equals (Object o) in order to get called internally by hashmap implementation?
h3r1n6
Profile Blog Joined September 2007
Iceland2039 Posts
Last Edited: 2008-04-10 23:01:42
April 10 2008 22:54 GMT
#5
The content is only the String "hale" with the measurement with value 6,2 as key.


Edit: Well this is the solution, its not going to help you much I guess. I'll explain:

HashTable, which implements the map interface, maps the keys you put in to specific values. Keys are compared with the compareTo(Object o) method, inherited by Object. In this case it will use the compareTo(Measurement m) method, defined in Measurement (can't formulate a correct explanation for this with my English ).
When adding the second object, the HashTable will find that key and replace the value with the new one (also returning the old value).
nobodyhome
Profile Blog Joined July 2003
United States139 Posts
April 10 2008 23:08 GMT
#6
the hash table should have both clancy and hale mapped to measurement (6, 2), but you wouldn't be able to access either of those. this is because since equals is defined incorrectly (needs to have object as a parameter), the second put() wouldn't overwrite the first.
bm
h3r1n6
Profile Blog Joined September 2007
Iceland2039 Posts
April 10 2008 23:12 GMT
#7
On April 11 2008 08:08 nobodyhome246 wrote:
the hash table should have both clancy and hale mapped to measurement (6, 2), but you wouldn't be able to access either of those. this is because since equals is defined incorrectly (needs to have object as a parameter), the second put() wouldn't overwrite the first.



Uh, wrong.

Measurement is an Object, and in this case Java will use the more specific method, which is compareTo(Measurement m).

Even if thats not the case, the map won't store 2 values for 1 key.
blagoonga123
Profile Blog Joined July 2007
United States2068 Posts
April 11 2008 03:24 GMT
#8
thanks for all the responses :0
FOOL! Pain is my friend! Now let me introduce you to it!
haduken
Profile Blog Joined April 2003
Australia8267 Posts
April 11 2008 09:25 GMT
#9
a decent hash implementation should always handle multiple keys...

in C and C++ templates, multiple objects per key is assumed.
so if your program should have any sophosication then you just need to reimplement your lookup function or method or wateva so it can check across keys with multiple objects.

not saying that should be done tho ~_~
Rillanon.au
r3dox
Profile Blog Joined May 2003
Germany261 Posts
April 11 2008 09:56 GMT
#10
On April 11 2008 08:12 h3r1n6 wrote:Uh, wrong.

Measurement is an Object, and in this case Java will use the more specific method, which is compareTo(Measurement m).

Even if thats not the case, the map won't store 2 values for 1 key.


wrong.

HashMap works with Objects of Static Type Object since it cannot know the Runtime Type of the objects it handles and thus, only equals(Object o) is getting called (if the hashcode matches).

here is a working version:
http://nopaste.info/15b02d1475.html

Output: {Measurement@bc=hale}

Also, HashMap will store 2 values for the "same" key if they have the same Hashcode, but do not return true in the equals Method.
You can try this by changing line 18 in the above code to "return false;"

Output: {Measurement@bc=hale, Measurement@bc=clancy}
h3r1n6
Profile Blog Joined September 2007
Iceland2039 Posts
April 11 2008 18:47 GMT
#11
Because if equals returns false then its not the same key... HashMap still won't store 2 values for 1 key.

But I was wrong with using the other method. Makes sense, because the map stores it as an Object and doesnt (need to) know the type. Man, I should think before I write answers.
Please log in or register to reply.
Live Events Refresh
Next event in 30m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
ProTech129
CosmosSc2 48
StarCraft: Brood War
Artosis 750
Aegong 65
Purpose 17
Dota 2
NeuroSwarm38
Counter-Strike
Stewie2K560
taco 450
Foxcn243
fl0m173
Other Games
summit1g7109
tarik_tv2665
Grubby2133
shahzam483
Day[9].tv391
Sick372
C9.Mang0303
ViBE203
ToD185
Mew2King46
Organizations
Other Games
BasetradeTV27
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 19 non-featured ]
StarCraft 2
• RyuSc2 56
• musti20045 54
• IndyKCrew
• LaughNgamezSOOP
• sooper7s
• AfreecaTV YouTube
• intothetv
• Kozan
• Migwel
StarCraft: Brood War
• Pr0nogo 1
• STPLYoutube
• ZZZeroYoutube
• iopq 0
• BSLYoutube
Dota 2
• masondota22060
League of Legends
• Doublelift3753
Counter-Strike
• Shiphtur197
Other Games
• imaqtpie1047
• Day9tv391
Upcoming Events
Replay Cast
30m
LiuLi Cup
11h 30m
MaxPax vs TriGGeR
ByuN vs herO
Cure vs Rogue
Classic vs HeRoMaRinE
Cosmonarchy
16h 30m
OyAji vs Sziky
Sziky vs WolFix
WolFix vs OyAji
Big Brain Bouts
16h 30m
Iba vs GgMaChine
TriGGeR vs Bunny
Reynor vs Classic
Serral vs Clem
BSL Team Wars
19h 30m
Team Hawk vs Team Dewalt
BSL Team Wars
19h 30m
Team Hawk vs Team Bonyth
Code For Giants Cup
23h
SC Evo League
1d 12h
TaeJa vs Cure
Rogue vs threepoint
ByuN vs Creator
MaNa vs Classic
Maestros of the Game
1d 16h
ShoWTimE vs Cham
GuMiho vs Ryung
Zoun vs Spirit
Rogue vs MaNa
[BSL 2025] Weekly
1d 18h
[ Show More ]
SC Evo League
2 days
Maestros of the Game
2 days
SHIN vs Creator
Astrea vs Lambo
Bunny vs SKillous
HeRoMaRinE vs TriGGeR
BSL Team Wars
2 days
Team Bonyth vs Team Sziky
BSL Team Wars
2 days
Team Dewalt vs Team Sziky
Monday Night Weeklies
3 days
Replay Cast
4 days
Sparkling Tuna Cup
4 days
LiuLi Cup
5 days
Replay Cast
6 days
The PondCast
6 days
RSL Revival
6 days
Maru vs SHIN
MaNa vs MaxPax
Liquipedia Results

Completed

CSL Season 18: Qualifier 1
uThermal 2v2 Main Event
HCC Europe

Ongoing

Copa Latinoamericana 4
BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Qualifiers
ASL Season 20
Acropolis #4 - TS1
CSL Season 18: Qualifier 2
SEL Season 2 Championship
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025

Upcoming

CSL 2025 AUTUMN (S18)
LASL Season 20
BSL Season 21
BSL 21 Team A
Chzzk MurlocKing SC1 vs SC2 Cup #2
RSL Revival: Season 2
Maestros of the Game
EC S1
Sisters' Call Cup
Skyesports Masters 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
MESA Nomadic Masters Fall
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.