• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

scjp chapter 7 self test doubt

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic