-
-
Save malte0811/b2ff3a1532a5081ec9a6466d50452f64 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.annotation.Nonnull; | |
import java.util.HashSet; | |
class BrokenHashDemo | |
{ | |
public static void main(String[] args) | |
{ | |
HashSet<Dumb> veryDumb = new HashSet<>(); | |
for (int i = 0;i<1000000;++i) { | |
veryDumb.add(new Dumb(i, i)); | |
} | |
/* | |
Output should be true, but is false. Java version: | |
java version "1.8.0_201" | |
Java(TM) SE Runtime Environment (build 1.8.0_201-b09) | |
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode) | |
*/ | |
System.out.println(veryDumb.contains(new Dumb(0, Integer.MAX_VALUE))); | |
} | |
static class Dumb implements Comparable<Dumb> { | |
int valEqual, valComp; | |
public Dumb(int valEqual, int valComp) { | |
this.valEqual = valEqual; | |
this.valComp = valComp; | |
} | |
@Override | |
public boolean equals(Object other) { | |
if (!(other instanceof Dumb)) return false; | |
return valEqual==((Dumb)other).valEqual; | |
} | |
@Override | |
public int hashCode() | |
{ | |
return 0; | |
} | |
@Override | |
public int compareTo(@Nonnull Dumb o) | |
{ | |
return Integer.compare(valComp, o.valComp); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment