• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 00:45
CEST 06:45
KST 13:45
  • 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
Classic Games #3: Rogue vs Serral at BlizzCon9[ASL20] Ro16 Preview Pt1: Ascent10Maestros of the Game: Week 1/Play-in Preview12[ASL20] Ro24 Preview Pt2: Take-Off7[ASL20] Ro24 Preview Pt1: Runway13
Community News
SC4ALL $6,000 Open LAN in Philadelphia7Weekly Cups (Sept 1-7): MaxPax rebounds & Clem saga continues24LiuLi Cup - September 2025 Tournaments3Weekly Cups (August 25-31): Clem's Last Straw?39Weekly Cups (Aug 18-24): herO dethrones MaxPax6
StarCraft 2
General
#1: Maru - Greatest Players of All Time Weekly Cups (Sept 1-7): MaxPax rebounds & Clem saga continues Team Liquid Map Contest #21 - Presented by Monster Energy Classic Games #3: Rogue vs Serral at BlizzCon What happened to Singapore/Brazil servers?
Tourneys
RSL: Revival, a new crowdfunded tournament series Sparkling Tuna Cup - Weekly Open Tournament SC4ALL $6,000 Open LAN in Philadelphia LANified! 37: Groundswell, BYOC LAN, Nov 28-30 2025 LiuLi Cup - September 2025 Tournaments
Strategy
Custom Maps
External Content
Mutation # 490 Masters of Midnight Mutation # 489 Bannable Offense Mutation # 488 What Goes Around Mutation # 487 Think Fast
Brood War
General
BW General Discussion BGH Auto Balance -> http://bghmmr.eu/ BSL Team Wars - Bonyth, Dewalt, Hawk & Sziky teams ASL20 General Discussion alas... i aint gon' lie to u bruh...
Tourneys
[ASL20] Ro16 Group B SC4ALL $1,500 Open Bracket LAN CPL12 SIGN UP are open!!! [Megathread] Daily Proleagues
Strategy
Simple Questions, Simple Answers Muta micro map competition Fighting Spirit mining rates [G] Mineral Boosting
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Path of Exile General RTS Discussion Thread Borderlands 3
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
Community
General
Effective ED Solutions for Better Relationships US Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread The Games Industry And ATVI
Fan Clubs
The Happy Fan Club!
Media & Entertainment
Movie Discussion! [Manga] One Piece Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread High temperatures on bridge(s)
TL Community
BarCraft in Tokyo Japan for ASL Season5 Final The Automated Ban List
Blogs
The Personality of a Spender…
TrAiDoS
A very expensive lesson on ma…
Garnet
hello world
radishsoup
Lemme tell you a thing o…
JoinTheRain
RTS Design in Hypercoven
a11
Evil Gacha Games and the…
ffswowsucks
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1374 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 15m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Nina 170
Livibee 18
StarCraft: Brood War
Sea 6456
Noble 62
sSak 53
ToSsGirL 50
Dota 2
LuMiX1
League of Legends
JimRising 630
Counter-Strike
Coldzera 287
Stewie2K247
PGG 97
semphis_40
Super Smash Bros
C9.Mang0366
Other Games
summit1g6884
shahzam670
WinterStarcraft291
XaKoH 208
ViBE53
Organizations
Other Games
gamesdonequick1672
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 16 non-featured ]
StarCraft 2
• Berry_CruncH213
• practicex 29
• Sammyuel 8
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• Diggity5
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Lourlo1005
• Stunt400
Upcoming Events
RSL Revival
5h 15m
Maestros of the Game
9h 15m
ShoWTimE vs Classic
Clem vs herO
Serral vs Bunny
Reynor vs Zoun
Cosmonarchy
11h 15m
Bonyth vs Dewalt
[BSL 2025] Weekly
13h 15m
RSL Revival
1d 5h
Maestros of the Game
1d 12h
BSL Team Wars
1d 14h
Afreeca Starleague
2 days
Snow vs Sharp
Jaedong vs Mini
Wardi Open
2 days
Sparkling Tuna Cup
3 days
[ Show More ]
Afreeca Starleague
3 days
Light vs Speed
Larva vs Soma
LiuLi Cup
4 days
The PondCast
5 days
Korean StarCraft League
6 days
Liquipedia Results

Completed

Proleague 2025-09-10
SEL Season 2 Championship
HCC Europe

Ongoing

BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Points
ASL Season 20
CSL 2025 AUTUMN (S18)
LASL Season 20
RSL Revival: Season 2
Maestros of the Game
Chzzk MurlocKing SC1 vs SC2 Cup #2
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

Upcoming

2025 Chongqing Offline CUP
BSL Polish World Championship 2025
BSL Season 21
SC4ALL: Brood War
BSL 21 Team A
SC4ALL: StarCraft II
EC S1
SL Budapest Major 2025
BLAST Rivals Fall 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
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.