• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 13:52
CET 19:52
KST 03:52
  • 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
HomeStory Cup 28 - Info & Preview12Rongyi Cup S3 - Preview & Info3herO wins SC2 All-Star Invitational14SC2 All-Star Invitational: Tournament Preview5RSL Revival - 2025 Season Finals Preview8
Community News
Weekly Cups (Jan 26-Feb 1): herO, Clem, ByuN, Classic win2RSL Season 4 announced for March-April7Weekly Cups (Jan 19-25): Bunny, Trigger, MaxPax win3Weekly Cups (Jan 12-18): herO, MaxPax, Solar win0BSL Season 2025 - Full Overview and Conclusion8
StarCraft 2
General
StarCraft 2 Not at the Esports World Cup 2026 Weekly Cups (Jan 26-Feb 1): herO, Clem, ByuN, Classic win HomeStory Cup 28 - Info & Preview Weekly Cups (Jan 19-25): Bunny, Trigger, MaxPax win Oliveira Would Have Returned If EWC Continued
Tourneys
RSL Season 4 announced for March-April PIG STY FESTIVAL 7.0! (19 Feb - 1 Mar) HomeStory Cup 28 StarCraft Evolution League (SC Evo Biweekly) $21,000 Rongyi Cup Season 3 announced (Jan 22-Feb 7)
Strategy
Custom Maps
[A] Starcraft Sound Mod
External Content
Mutation # 511 Temple of Rebirth The PondCast: SC2 News & Results Mutation # 510 Safety Violation Mutation # 509 Doomsday Report
Brood War
General
[ASL21] Potential Map Candidates Can someone share very abbreviated BW cliffnotes? BW General Discussion Liquipedia.net NEEDS editors for Brood War BGH Auto Balance -> http://bghmmr.eu/
Tourneys
[Megathread] Daily Proleagues Azhi's Colosseum - Season 2 Small VOD Thread 2.0 [BSL21] Non-Korean Championship - Starts Jan 10
Strategy
Zealot bombing is no longer popular? Simple Questions, Simple Answers Current Meta Soma's 9 hatch build from ASL Game 2
Other Games
General Games
Nintendo Switch Thread Battle Aces/David Kim RTS Megathread Path of Exile Mobile Legends: Bang Bang Beyond All Reason
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Join illminati in Luanda Angola+27 60 696 7068
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
Mafia Game Mode Feedback/Ideas Vanilla Mini Mafia
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine European Politico-economics QA Mega-thread The Games Industry And ATVI Canadian Politics Mega-thread
Fan Clubs
The herO Fan Club! The IdrA Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece
Sports
2024 - 2026 Football Thread
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
Play, Watch, Drink: Esports …
TrAiDoS
My 2025 Magic: The Gathering…
DARKING
Life Update and thoughts.
FuDDx
How do archons sleep?
8882
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1432 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 5h 8m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
mouzHeroMarine 365
Harstem 255
OGKoka 164
UpATreeSC 98
BRAT_OK 65
JuggernautJason17
MindelVK 15
StarCraft: Brood War
Britney 23664
Calm 3272
Mini 526
Shuttle 165
actioN 150
ggaemo 114
Backho 51
Hyuk 36
910 28
Rock 16
Dota 2
qojqva2066
Dendi804
LuMiX1
League of Legends
C9.Mang067
Counter-Strike
fl0m1737
adren_tv114
ptr_tv84
Heroes of the Storm
Liquid`Hasu310
Khaldor131
Other Games
gofns12021
Grubby3902
FrodaN1846
Beastyqt904
ArmadaUGS107
TKL 102
Mew2King58
Trikslyr55
Organizations
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 20 non-featured ]
StarCraft 2
• StrangeGG 74
• HeavenSC 25
• Reevou 5
• Kozan
• Migwel
• AfreecaTV YouTube
• sooper7s
• intothetv
• IndyKCrew
• LaughNgamezSOOP
StarCraft: Brood War
• HerbMon 33
• FirePhoenix11
• Michael_bg 2
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• WagamamaTV461
League of Legends
• TFBlade1601
• imaqtpie1544
• Shiphtur388
Upcoming Events
Replay Cast
5h 8m
The PondCast
15h 8m
WardiTV Invitational
17h 8m
Replay Cast
1d 5h
RongYI Cup
2 days
herO vs Maru
uThermal 2v2 Circuit
3 days
Replay Cast
4 days
Wardi Open
4 days
Monday Night Weeklies
4 days
Sparkling Tuna Cup
5 days
Liquipedia Results

Completed

Proleague 2026-02-03
HSC XXVIII
Underdog Cup #3

Ongoing

CSL 2025 WINTER (S19)
KCM Race Survival 2026 Season 1
Acropolis #4 - TS4
Rongyi Cup S3
Nations Cup 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
eXTREMESLAND 2025
SL Budapest Major 2025
ESL Impact League Season 8

Upcoming

Escore Tournament S1: W7
Escore Tournament S1: W8
Acropolis #4
IPSL Spring 2026
HSC XXIX
uThermal 2v2 2026 Main Event
Bellum Gens Elite Stara Zagora 2026
RSL Revival: Season 4
LiuLi Cup: 2025 Grand Finals
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League Season 23
ESL Pro League Season 23
PGL Cluj-Napoca 2026
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 © 2026 TLnet. All Rights Reserved.