Codice:
class Couple{
Integer key;
Object value;
}
public class MyHashTable{
private Couple[] table;
private int size;
private static final int DEF_SIZE=100;
public MyHashTable(int size){
this.size=size;
this.table=new Couple[this.size];
}
public MyHashTable(){
this(DEF_SIZE);
}
public boolean put(Integer key,Object value){
for (int i=0;i<this.table.length;i++){
int pos=calcolaHash(key,this.size,i);
if (this.table[pos]==null){
Couple coup=new Couple();
coup.key=key;
coup.value=value;
this.table[pos]=coup;
return true;
}
}
return false;
}
private static int calcolaHash(Integer key,int size,int i){
return ((key.intValue()%size)+i)%size;
}
}
Questo dovrebbe funzionare