Last active
February 18, 2016 13:53
-
-
Save gofer/2b7cee23de018f5b3853 to your computer and use it in GitHub Desktop.
なぜJavaのObjectには#hashCode()があるのか… (歴史的経緯?)
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
//package info.ex-studio.gist; | |
import java.util.*; | |
import java.lang.*; | |
import java.io.*; | |
interface Hashable<T> | |
{ | |
T get(); | |
void set(T value); | |
int hash(); | |
} | |
class IntegerHashItem implements Hashable<Integer> | |
{ | |
Integer _value; | |
public IntegerHashItem(Integer value) { _value = value; } | |
public Integer get() { return _value; } | |
public void set(Integer value) { _value = value; } | |
public int hash() { return _value; } | |
} | |
class MyHash<Item extends Hashable> | |
{ | |
MyHash() | |
{ | |
System.out.println("new MyHash()"); | |
} | |
public void add(Item item) | |
{ | |
System.out.printf("%d[:%d]\n", item.get(), item.hash()); | |
} | |
} | |
class Main | |
{ | |
public static void main (String[] args) throws java.lang.Exception | |
{ | |
MyHash h = new MyHash(); | |
h.add( | |
new IntegerHashItem( | |
new Integer(3) | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment