• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 04:25
CET 10:25
KST 18:25
  • 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
RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10
Community News
[BSL21] Ro.16 Group Stage (C->B->A->D)4Weekly Cups (Nov 17-23): Solar, MaxPax, Clem win2RSL Season 3: RO16 results & RO8 bracket13Weekly Cups (Nov 10-16): Reynor, Solar lead Zerg surge2[TLMC] Fall/Winter 2025 Ladder Map Rotation14
StarCraft 2
General
When will we find out if there are more tournament Weekly Cups (Nov 17-23): Solar, MaxPax, Clem win SC: Evo Complete - Ranked Ladder OPEN ALPHA Weekly Cups (Nov 10-16): Reynor, Solar lead Zerg surge RSL Season 3: RO16 results & RO8 bracket
Tourneys
Tenacious Turtle Tussle [Alpha Pro Series] Nice vs Cure RSL Revival: Season 3 $5,000+ WardiTV 2025 Championship StarCraft Evolution League (SC Evo Biweekly)
Strategy
Custom Maps
kubetno179 Map Editor closed ?
External Content
Mutation # 501 Price of Progress Mutation # 500 Fright night Mutation # 499 Chilling Adaptation Mutation # 498 Wheel of Misfortune|Cradle of Death
Brood War
General
Data analysis on 70 million replays Which season is the best in ASL? [BSL21] Ro.16 Group Stage (C->B->A->D) FlaSh on: Biggest Problem With SnOw's Playstyle soO on: FanTaSy's Potential Return to StarCraft
Tourneys
[Megathread] Daily Proleagues [BSL21] RO16 Tie Breaker - Group B - Sun 21:00 CET [BSL21] GosuLeague T1 Ro16 - Tue & Thu 22:00 CET [BSL21] RO16 Tie Breaker - Group A - Sat 21:00 CET
Strategy
Game Theory for Starcraft How to stay on top of macro? Current Meta PvZ map balance
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Beyond All Reason The Perfect Game Should offensive tower rushing be viable in RTS games?
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
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Mafia Game Mode Feedback/Ideas
Community
General
Russo-Ukrainian War Thread The China Politics Thread US Politics Mega-thread Artificial Intelligence Thread Things Aren’t Peaceful in Palestine
Fan Clubs
White-Ra Fan Club
Media & Entertainment
[Manga] One Piece Movie Discussion! Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion NBA General Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Where to ask questions and add stream? The Automated Ban List
Blogs
Thanks for the RSL
Hildegard
The Health Impact of Joining…
TrAiDoS
Saturation point
Uldridge
DnB/metal remix FFO Mick Go…
ImbaTosS
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2006 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 35m
[ Submit Event ]
Live Streams
Refresh
StarCraft: Brood War
Shuttle 360
Soma 198
Hyun 186
EffOrt 168
Zeus 143
Dewaltoss 106
zelot 63
Light 62
sorry 60
Sacsri 39
[ Show more ]
Noble 32
ToSsGirL 31
Sharp 17
Bale 9
Sexy 6
Terrorterran 4
Pusan 2
Rain 1
Dota 2
NeuroSwarm125
XcaliburYe95
League of Legends
JimRising 455
Counter-Strike
olofmeister425
shoxiejesuss223
Other Games
summit1g15601
ceh9543
C9.Mang0210
Fuzer 189
Mew2King78
Organizations
Other Games
gamesdonequick658
Dota 2
PGL Dota 2 - Main Stream240
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 17 non-featured ]
StarCraft 2
• LUISG 38
• Light_VIP 6
• IndyKCrew
• sooper7s
• AfreecaTV YouTube
• Migwel
• intothetv
• Kozan
• LaughNgamezSOOP
StarCraft: Brood War
• iopq 4
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• lizZardDota245
League of Legends
• Lourlo1783
• Jankos846
• Stunt575
Upcoming Events
The PondCast
35m
Replay Cast
13h 35m
OSC
1d 7h
LAN Event
1d 8h
Replay Cast
1d 13h
Replay Cast
1d 23h
WardiTV Korean Royale
2 days
Sparkling Tuna Cup
3 days
WardiTV Korean Royale
3 days
Replay Cast
3 days
[ Show More ]
Wardi Open
4 days
Monday Night Weeklies
4 days
StarCraft2.fi
4 days
Replay Cast
4 days
Wardi Open
5 days
StarCraft2.fi
5 days
Wardi Open
6 days
StarCraft2.fi
6 days
Liquipedia Results

Completed

SOOP Univ League 2025
RSL Revival: Season 3
Eternal Conflict S1

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
YSL S2
BSL Season 21
CSCL: Masked Kings S3
Slon Tour Season 2
META Madness #9
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2

Upcoming

BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter 2026: Closed Qualifier
eXTREMESLAND 2025
ESL Impact League Season 8
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.