class Point{ int x, y, l; Point(int x, int y, int l){ this.x =x; this.y =y; this.l=l; } @Override public int hashCode() { return y; } @Override public boolean equals(final Object obj) { if(this == obj) return true; if(!(obj instanceof Point)) return false; Point p = (Point) obj; return this.x == px && this.y == py; } } TreeMap<Point,Integer> sortedMap = new TreeMap<>((p1, p2)-> p1.l-p2.l); sortedMap.put(new Point(4,5,0),0); sortedMap.put(new Point(5,5,0),6); System.out.println(sortedMap.size()); -> Output: 1 System.out.println((new Point(4,5,0)).equals(new Point(5,5,0))); -> Output -> False.
I overloaded both hash codes equal to the method in the class. I think the put method should use the equals method to determine if the same object exits or not. But it does not work as expected.
I'm not sure why my hashMap size is 1 .. Please let me know if I realized that hashmap is not working properly. Help me identify an error in this code?
source share