cours_progra/bac1/q2/algo/hashmap.java
Debucquoy 4fd7542f03
.
2023-09-20 15:18:20 +02:00

50 lines
1.0 KiB
Java

public class hashmap implements Map{
public static final TAB_SIZE = 1024;
LinkedList[] values = new Object[TAB_SIZE];
class HashEntry{ public Object key, values;
public HashEntry(Object key, Object value){
this.key = key;
this.value = value;
}
}
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<HashEntry> t){
t.add(new HashEntry(key, value));
return;
}
Object temp = values[pos];
values[pos] = new ArrayList<HashEntry>;
value[pos].add(temp);
value[pos].add(new HashEntry(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<HashEntry> t){
for (v : t) {
if (v.key.equals(key)){
return v.value
}
}
}
return null;
}
public static void main(String[] args) {
System.out.println("test");
}
}