Prev: Call for Papers Reminder (extended): The World Congress on Engineering WCE 2010
Next: Exception Handling
From: Javas on 9 Mar 2010 05:09 import java.util.*; public class Qn353 { private String name; public Qn353(String name) { this.name = name; } public boolean equals(Object o) { if ( ! (o instanceof Qn353) ) return false; Qn353 p = (Qn353) o; return p.name.equals(this.name); } public static void main(String [] args) { HashSet<Object> hs = new HashSet<Object>(); hs.add(p1); hs.add(p2); System.out.println(hs.size()); } public int hashcode() { return 1; } } Above code outputs : 2 But I thought it is supossed to be 1 since the hashcode is the same for both the objects, equals also holds true and the set doesnt allow duplicates. What is wrong? Please correct me. Thanks!
From: RedGrittyBrick on 9 Mar 2010 05:34 On 09/03/2010 10:09, Javas wrote: > import java.util.*; > public class Qn353 > { > private String name; > public Qn353(String name) > { > this.name = name; > } > > public boolean equals(Object o) > { > if ( ! (o instanceof Qn353) ) return false; > Qn353 p = (Qn353) o; > return p.name.equals(this.name); > } > public static void main(String [] args) > { > HashSet<Object> hs = new HashSet<Object>(); > hs.add(p1); > hs.add(p2); Where did p1 and p2 spring from? Does this compile? I'd expect the Java compiler to be a bit puzzled about the nature of p1 and p2 at this point. > System.out.println(hs.size()); > } > public int hashcode() > { > return 1; > } > > > > } > > > Above code outputs : 2 Some other code might but I don't see how this code can run. > But I thought it is supossed to be 1 since the hashcode is the same > for both the objects, equals also holds true and the set doesnt allow > duplicates. What is wrong? Please correct me. Maybe you're assuming p1 and p2 are instances of Qn353 when they are not. -- RGB
From: Roedy Green on 9 Mar 2010 06:43 On Tue, 9 Mar 2010 02:09:09 -0800 (PST), Javas <deepan.17(a)gmail.com> wrote, quoted or indirectly quoted someone who said : >Above code outputs : 2 >But I thought it is supossed to be 1 since the hashcode is the same >for both the objects, equals also holds true and the set doesnt allow >duplicates. What is wrong? Please correct me. The hashCode has nothing whatever to do with what is considered a duplicate. Setting the hashCode to 1 just destroys the efficiency of the HashCode lookup. Keep in mind that even in ordinary use, HashCodes are not unique, and even less after having the high bits trimmed off to index the lookup table. see http://mindprod.com/jgloss/hashcode.html -- Roedy Green Canadian Mind Products http://mindprod.com The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. ~ Tom Cargill
From: Roedy Green on 9 Mar 2010 06:44 On Tue, 9 Mar 2010 02:09:09 -0800 (PST), Javas <deepan.17(a)gmail.com> wrote, quoted or indirectly quoted someone who said : >import java.util.*; >public class Qn353 >{ > private String name; > public Qn353(String name) > { > this.name = name; > } > > public boolean equals(Object o) > { > if ( ! (o instanceof Qn353) ) return false; > Qn353 p = (Qn353) o; > return p.name.equals(this.name); > } > public static void main(String [] args) > { > HashSet<Object> hs = new HashSet<Object>(); > hs.add(p1); > hs.add(p2); > System.out.println(hs.size()); > } > public int hashcode() > { > return 1; > } This would not compile. You never define p1 and p2. -- Roedy Green Canadian Mind Products http://mindprod.com The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. ~ Tom Cargill
From: Andreas Leitgeb on 9 Mar 2010 08:08 Javas <deepan.17(a)gmail.com> wrote: > public int hashcode() > { > return 1; > } Defining a method hashcode() has no effect on Collections at all, because they call hashCode() (with capital C), instead. The other comments (p1 and p2 undefined, and the actual effect of overriding correctly spelled hashCode() to return a constant) also apply.
|
Next
|
Last
Pages: 1 2 Prev: Call for Papers Reminder (extended): The World Congress on Engineering WCE 2010 Next: Exception Handling |