alebu
Programmer
- Sep 7, 2002
- 46
Do I understand correctly that 2 equals objects MUST return the same hashCode() value BUT if 2 objects has the same hashCode it is DOES'NT mean that they are equals?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
class Usbstick
{
private int price;
public int hashCode () { return price; }
public Usbstick (int p) { price = p; }
}
class User
{
private int id;
private int age;
// not the best implementation:
public int hashCode () { return 100*age + id; }
public User (int i, int a) { age = a; id = i; }
}
public UserHash (String param)
{
User u1 = new User (100, 0);
User u2 = new User (0, 1);
Usbstick u3 = new Usbstick (100);
System.out.println (u1.hashCode () + " " + u2.hashCode () + " " + u3.hashCode ());
}