Created
September 9, 2014 22:43
-
-
Save coekie/aae90cefdbae7c39af23 to your computer and use it in GitHub Desktop.
Find Kirk
This file contains hidden or 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 java.io.PrintStream; | |
// Makes it find Kirk every time. | |
// Works for me every time... but might be system dependent. | |
public class FindKirk { | |
public static void main(String[] args) throws InterruptedException { | |
// warmup, avoiding that initialization taking an identity hash code gets in our way | |
JavaChampionTest.main(); | |
// hash code 2134400190 is an early twin. 1802421938 comes 2 before the first occurrence, | |
// so run up to that one; then Jack will take the one in between, and the first Kirk gets the | |
// first of the twin | |
while (new Object().hashCode() != 1802421938); | |
// override System.out to do something between the creating of the first and the second Kirk | |
// (without changing the original code) | |
System.setOut(new PrintStream(System.out) { | |
boolean done; | |
@Override public void println(String x) { | |
// 760371604 comes just before the second occurrence of the twin | |
while (!done && new Object().hashCode() != 760371604); | |
done = true; | |
super.println(x); | |
} | |
}); | |
JavaChampionTest.main(); | |
} | |
} |
This file contains hidden or 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 java.util.*; | |
// From http://www.javaspecialists.eu/archive/Issue222.html | |
public class JavaChampionTest { | |
public static void main(String... args) { | |
Map<JavaChampion, String> urls = new HashMap<>(); | |
urls.put(new JavaChampion("Jack"), "fasterj.com"); | |
urls.put(new JavaChampion("Kirk"), "kodewerk.com"); | |
urls.put(new JavaChampion("Heinz"), "javaspecialists.eu"); | |
urls.forEach((p, u) -> System.out.println(u)); // Java 8 | |
System.out.println("URL for Kirk: " + | |
urls.get(new JavaChampion("Kirk"))); | |
} | |
} | |
final class JavaChampion { | |
private final String name; | |
public JavaChampion(String name) { | |
this.name = name; | |
} | |
public boolean equals(Object o) { // simplified equals() | |
if (!(o instanceof JavaChampion)) return false; | |
return name.equals(((JavaChampion) o).name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment