cours_progra/q2/algo/hashmap.java

49 lines
959 B
Java
Raw Permalink Normal View History

2023-04-26 11:24:13 +02:00
public class hashmap implements Map{
public static final TAB_SIZE = 1024;
Object[] values = new Object[TAB_SIZE];
class tuple{ public Object key, values;
public tuple(Object key, Object value){
this.key = key;
this.value = value;
}
}
public hashmap() {
}
public void put(Object key, Object value){
int pos = key.hashCode() % values.lenght();
if (values[pos] == null){
values[pos] = value;
return;
}
if(value[pos] instanceof ArrayList<tuple> t){
t.add(new tuple(key, value));
return;
}
Object temp = values[pos];
values[pos] = new ArrayList<tuple>;
value[pos].add(temp);
value[pos].add(new tuple(key, value));
}
public Object get(Object key){
int pos = key.hashCode() % values.lenght();
if(values[pos].equals(key)){
return values[pos].value;
}
if(values[pos] instanceof ArrayList<tuple> t){
for (v : t) {
if (v.key.equals(key)){
return v.value
}
}
}
return null;
}
}