Skip to content

Instantly share code, notes, and snippets.

@ivmos
Created May 19, 2017 10:26
Show Gist options
  • Save ivmos/8657c533540b3965b6b8d925a8fecad9 to your computer and use it in GitHub Desktop.
Save ivmos/8657c533540b3965b6b8d925a8fecad9 to your computer and use it in GitHub Desktop.
// More about hash code
import java.util.*;
import java.lang.*;
public class Example
{
public int x;
public Example(int x) {
this.x = x;
}
public static void main(String[] args)
{
Set<Example> set = new HashSet<>();
long startTime = System.nanoTime();
Example last = null;
for (int i=0; i < 10; i++) {
last = new Example(i);
set.add(last);
}
long estimatedTime = System.nanoTime() - startTime;
System.out.println(set.size());
System.out.println((double)estimatedTime / 1000000000.0);
Example other = new Example(-1);
System.out.println(set.contains(other));
}
public int hashCode() {
return 2;
}
public boolean equals(Object a) {
Example aE = (Example) a;
System.out.println("equals " + this.x + " " + aE.x);
return super.equals(a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment