• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hashset & overriding hashcode()

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: myown code(model from K&B)

-----



Inspite of overriding hashcode() and also equals,how does this hashset allow test6 objects with same count(variable) to be added?could you please spot what is wrong with this code?

Thanks in advance.
 
vinal sen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgot to add,after initializing elements:


Output: 3,5,5

Please explain.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Java is case-sensitive. You didn't actually override hashCode method. You just create a new method "public int hashcode()" which is not called when an object is added to HashSet. So the current hashCode method, which is called by the collection, is the one inherited from Object class.


Viorel
 
vinal sen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THANK YOU VIOREL!
 
Viorel Craescu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
import java.text.*;
public class test6
{ /** * @param args */
int count;
test6(int s)
{count =s;
}
public static void main(String[] args)
{// TODO Auto-generated method stub
HashSet<test6> set=new HashSet<test6>();
set.add(new test6(5));
set.add(new test6(5));
set.add(new test6(3));

for (test6 t: set)
System.out.println(t.count);
}
public boolean equals(Object o)
{
return (this.count== ((test6)o).count);
}
public int hashcode()
{
return count;
}

}

O/P --

5
5
3

Not able to get. Can anyone please explain me this in simple terms. Please...
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

To add to what Viorel has pointed out, I suggest that if you are using JDK 5 or above, you can make use of @Override annotation to catch such errors at compile-time.

Best regards,

- Aditya Jha
 
vinal sen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code is trying to add duplicate Objects(of test6) which you consider meaningfully equal.so you try to override both the equals() and hashCode() and say that if the "count" variable values are same,they will be equal and should be considered duplicates.As it is a set,it wont add duplicates.

The typo I made: hashcode() instead hashCode() with capital 'C'.it did not override and it allowed duplicate objects.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic