• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 15:13
CEST 21:13
KST 04:13
  • 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
[ASL19] Finals Recap: Standing Tall9HomeStory Cup 27 - Info & Preview18Classic wins Code S Season 2 (2025)16Code S RO4 & Finals Preview: herO, Rogue, Classic, GuMiho0TL Team Map Contest #5: Presented by Monster Energy6
Community News
Flash Announces Hiatus From ASL56Weekly Cups (June 23-29): Reynor in world title form?13FEL Cracov 2025 (July 27) - $8000 live event19Esports World Cup 2025 - Final Player Roster16Weekly Cups (June 16-22): Clem strikes back1
StarCraft 2
General
Statistics for vetoed/disliked maps The SCII GOAT: A statistical Evaluation Weekly Cups (June 23-29): Reynor in world title form? PiG Sty Festival #5: Playoffs Preview + Groups Recap The GOAT ranking of GOAT rankings
Tourneys
FEL Cracov 2025 (July 27) - $8000 live event RSL: Revival, a new crowdfunded tournament series Korean Starcraft League Week 77 Master Swan Open (Global Bronze-Master 2) [GSL 2025] Code S: Season 2 - Semi Finals & Finals
Strategy
How did i lose this ZvP, whats the proper response Simple Questions Simple Answers
Custom Maps
[UMS] Zillion Zerglings
External Content
Mutation # 480 Moths to the Flame Mutation # 479 Worn Out Welcome Mutation # 478 Instant Karma Mutation # 477 Slow and Steady
Brood War
General
Flash Announces Hiatus From ASL Player “Jedi” cheat on CSL Replays question BW General Discussion BGH Auto Balance -> http://bghmmr.eu/
Tourneys
[Megathread] Daily Proleagues [BSL20] Grand Finals - Sunday 20:00 CET Small VOD Thread 2.0 [BSL20] GosuLeague RO16 - Tue & Wed 20:00+CET
Strategy
Simple Questions, Simple Answers I am doing this better than progamers do.
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Path of Exile What do you want from future RTS games? Beyond All Reason
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
Russo-Ukrainian War Thread US Politics Mega-thread Trading/Investing Thread Things Aren’t Peaceful in Palestine The Games Industry And ATVI
Fan Clubs
SKT1 Classic Fan Club! Maru Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece [\m/] Heavy Metal Thread
Sports
2024 - 2025 Football Thread Formula 1 Discussion NBA General Discussion TeamLiquid Health and Fitness Initiative For 2023 NHL Playoffs 2024
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Blogs
Culture Clash in Video Games…
TrAiDoS
from making sc maps to makin…
Husyelt
Blog #2
tankgirl
StarCraft improvement
iopq
Trip to the Zoo
micronesia
Customize Sidebar...

Website Feedback

Closed Threads



Active: 645 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
FEL
16:00
Cracov 2025: Qualifier #1
RotterdaM865
IndyStarCraft 276
CranKy Ducklings226
Liquipedia
PSISTORM Gaming Misc
15:55
FSL team league: ASP vs PTB
Freeedom9
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 865
IndyStarCraft 276
JuggernautJason82
ProTech60
StarCraft: Brood War
Calm 4614
firebathero 298
JulyZerg 133
BRAT_OK 95
Rock 38
LancerX 10
Stormgate
Nathanias37
Dota 2
monkeys_forever200
LuMiX1
League of Legends
Grubby2526
Dendi1486
Counter-Strike
fl0m1895
Super Smash Bros
Westballz39
Heroes of the Storm
Khaldor330
Liquid`Hasu266
Other Games
FrodaN1489
Mlord584
KnowMe135
Sick46
Trikslyr46
Organizations
Other Games
EGCTV1358
StarCraft 2
angryscii 18
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 24 non-featured ]
StarCraft 2
• Berry_CruncH71
• printf 70
• tFFMrPink 24
• iHatsuTV 9
• OhrlRock 3
• Kozan
• Migwel
• sooper7s
• AfreecaTV YouTube
• intothetv
• IndyKCrew
• LaughNgamezSOOP
StarCraft: Brood War
• Azhi_Dahaki34
• Pr0nogo 1
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• masondota2806
• WagamamaTV663
• Ler97
League of Legends
• Jankos1790
• Doublelift1602
Other Games
• imaqtpie998
• Shiphtur491
Upcoming Events
RSL Revival
14h 47m
Clem vs Classic
SHIN vs Cure
FEL
16h 47m
WardiTV European League
16h 47m
BSL: ProLeague
22h 47m
Dewalt vs Bonyth
Replay Cast
2 days
Sparkling Tuna Cup
2 days
WardiTV European League
2 days
The PondCast
3 days
Replay Cast
4 days
RSL Revival
4 days
[ Show More ]
Replay Cast
5 days
RSL Revival
5 days
FEL
5 days
RSL Revival
6 days
FEL
6 days
FEL
6 days
Liquipedia Results

Completed

BSL 2v2 Season 3
HSC XXVII
Heroes 10 EU

Ongoing

JPL Season 2
BSL Season 20
Acropolis #3
KCM Race Survival 2025 Season 2
CSL 17: 2025 SUMMER
Copa Latinoamericana 4
Championship of Russia 2025
RSL Revival: Season 1
Murky Cup #2
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25
BLAST Rivals Spring 2025
MESA Nomadic Masters
CCT Season 2 Global Finals
IEM Melbourne 2025

Upcoming

2025 ACS Season 2: Qualifier
CSLPRO Last Chance 2025
2025 ACS Season 2
CSLPRO Chat StarLAN 3
K-Championship
uThermal 2v2 Main Event
SEL Season 2 Championship
FEL Cracov 2025
Esports World Cup 2025
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
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.