Skip to content

Instantly share code, notes, and snippets.

@malte0811
Last active March 24, 2019 12:55
Show Gist options
  • Save malte0811/b2ff3a1532a5081ec9a6466d50452f64 to your computer and use it in GitHub Desktop.
Save malte0811/b2ff3a1532a5081ec9a6466d50452f64 to your computer and use it in GitHub Desktop.
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