cours_progra/bac1/q2/algo/hashmap.java

50 lines
1.0 KiB
Java
Raw Normal View History

2023-04-26 11:24:13 +02:00
public class hashmap implements Map{
public static final TAB_SIZE = 1024;
2023-09-20 15:18:20 +02:00
LinkedList[] values = new Object[TAB_SIZE];
class HashEntry{ public Object key, values;
public HashEntry(Object key, Object value){
2023-04-26 11:24:13 +02:00
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;
}
2023-09-20 15:18:20 +02:00
if(value[pos] instanceof ArrayList<HashEntry> t){
t.add(new HashEntry(key, value));
2023-04-26 11:24:13 +02:00
return;
}
Object temp = values[pos];
2023-09-20 15:18:20 +02:00
values[pos] = new ArrayList<HashEntry>;
2023-04-26 11:24:13 +02:00
value[pos].add(temp);
2023-09-20 15:18:20 +02:00
value[pos].add(new HashEntry(key, value));
2023-04-26 11:24:13 +02:00
}
public Object get(Object key){
int pos = key.hashCode() % values.lenght();
if(values[pos].equals(key)){
return values[pos].value;
}
2023-09-20 15:18:20 +02:00
if(values[pos] instanceof ArrayList<HashEntry> t){
2023-04-26 11:24:13 +02:00
for (v : t) {
if (v.key.equals(key)){
return v.value
}
}
}
return null;
}
2023-09-20 15:18:20 +02:00
public static void main(String[] args) {
System.out.println("test");
}
}