Skip to content

Instantly share code, notes, and snippets.

@gofer
Last active February 18, 2016 13:53
Show Gist options
  • Save gofer/2b7cee23de018f5b3853 to your computer and use it in GitHub Desktop.
Save gofer/2b7cee23de018f5b3853 to your computer and use it in GitHub Desktop.
なぜJavaのObjectには#hashCode()があるのか… (歴史的経緯?)
//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