Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

scjp chapter 7 self test doubt

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As we know in map duplicate keys are not allowed but in the following question duplicate keys are there.


import java.util.*;
class MapEQ {
public static void main(String[] args) {
Map<ToDos, String> m = new HashMap<ToDos, String>();
ToDos t1 = new ToDos("Monday");
ToDos t2 = new ToDos("Monday");

ToDos t3 = new ToDos("Tuesday");
m.put(t1, "doLaundry");
m.put(t2, "payBills");
m.put(t3, "cleanAttic");
System.out.println(m.size());
}
}
class ToDos{
String day;
ToDos(String d) { day = d; }
public boolean equals(Object o) {
return ((ToDos)o).day == this.day;
}
// public int hashCode() { return 9; }
}
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, ToDos has its hashCode() commented out. When you override equals(), you need to override hashCode() too. If you don't, you'll get strange results if you use those objects in HashMaps and HashSets.

It looks like it should behave correctly once you uncomment the hashCode() line.
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt how to dowload the link which you have given, do we need to download the Jar file or exe or launch the Java web start. Please help out..


[BPSouther: Removed code tags from non-code text]
[ May 05, 2008: Message edited by: Ben Souther ]
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Dinesh Tahiliani:
Matt how to dowload the link which you have given, do we need to download the Jar file or exe or launch the Java web start. Please help out...


Ooh, well, both should work. Drop me a private message if you can't get it to work (to save clogging up this forum with off-topic stuff).
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Matt Russell wrote:Well, ToDos has its hashCode() commented out. When you override equals(), you need to override hashCode() too. If you don't, you'll get strange results if you use those objects in HashMaps and HashSets.

It looks like it should behave correctly once you uncomment the hashCode() line.



I thought I understood this question until I read your explaination. Now I have a much clearer understanding and now the book's explaination makes more sense. (K&B SCJP6)
thanks
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add a pair of key/value which is already in the Map.
But the old key/valuepair will be replaced by the new one and the method put will return old value.
When you add the new pair the method put will return null.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, ToDos has its hashCode() commented out. When you override equals(), you need to override hashCode() too. If you don't, you'll get strange results if you use those objects in HashMaps and HashSets.



I think what the K&B book says about that strange behavior helps a lot:

If hashCode is not overridden then every entry will go into its own bucket



If I understand how HashMaps work, first, they use hashCode() to find the bucket and after that they use equals to see if the element's key we are trying to add is already there. With the hashCode() commented the default hasCode() will most likely turn a different value/bucket per each object. Therefore, there will be one object per bucket and the HashMap won't realize the key is duplicated.
 
    Bookmark Topic Watch Topic
  • New Topic