import java.util.*;
interface IDictionary<K, V> {
void insert(K key, V value);
V get(K key);
int size();
boolean isEmpty();
}
class Entry<K, V> {
K key;
V value;
public Entry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
return key.toString() + "#" + value.toString();
}
}
class Dictionary<K, V> implements IDictionary<K, V> {
private int size = 0;
private int tableSize = 10;
table
= new Object[tableSize
]; }
public void insert(K key, V value) {
int hash = key.hashCode();
hash = (hash & 0x7fffffff) % tableSize;
if(table[hash] != null) {
System.
out.
println("udpate 😂"); table[hash] = new Entry(key, value);
} else {
System.
out.
println("Inserted 😁"); table[hash] = new Entry(key, value);
size += 1;
}
// System.out.println(hash);
}
public V get(K key) {
int hash = key.hashCode();
hash = (hash & 0x7fffffff) % tableSize;
Entry e = (Entry)table[hash];
if(e == null) {
return null;
}
return (V)e.value;
}
@Override
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
}
class Main {
public static void main
(String[] args
) { Dictionary
<String, String
> dictionary
= new Dictionary
<>(); dictionary.insert("AMITH", "K");
dictionary.insert("AMITH", "K");
System.
out.
println(dictionary.
size()); dictionary.insert("LUFFY", "K");
System.
out.
println(dictionary.
size()); System.
out.
println(dictionary.
get("LUFFY")); dictionary.insert("LUFFY", "AMITH");
System.
out.
println(dictionary.
size()); System.
out.
println(dictionary.
get("LUFFY")); }
}
aW1wb3J0IGphdmEudXRpbC4qOwoKaW50ZXJmYWNlIElEaWN0aW9uYXJ5PEssIFY+IHsKICAgIHZvaWQgaW5zZXJ0KEsga2V5LCBWIHZhbHVlKTsKICAgIFYgZ2V0KEsga2V5KTsKICAgIGludCBzaXplKCk7CiAgICBib29sZWFuIGlzRW1wdHkoKTsKfQoKY2xhc3MgRW50cnk8SywgVj4gewogICAgSyBrZXk7CiAgICBWIHZhbHVlOwogICAgcHVibGljIEVudHJ5KEsga2V5LCBWIHZhbHVlKSB7CiAgICAgICAgdGhpcy5rZXkgPSBrZXk7CiAgICAgICAgdGhpcy52YWx1ZSA9IHZhbHVlOwogICAgfQoKICAgIEBPdmVycmlkZQogICAgcHVibGljIFN0cmluZyB0b1N0cmluZygpIHsKICAgICAgICByZXR1cm4ga2V5LnRvU3RyaW5nKCkgKyAiIyIgKyB2YWx1ZS50b1N0cmluZygpOwogICAgfQp9CgpjbGFzcyBEaWN0aW9uYXJ5PEssIFY+IGltcGxlbWVudHMgSURpY3Rpb25hcnk8SywgVj4gewogICAgcHJpdmF0ZSBpbnQgc2l6ZSA9IDA7CiAgICBwcml2YXRlIGludCB0YWJsZVNpemUgPSAxMDsKICAgIHByaXZhdGUgT2JqZWN0IHRhYmxlW107CiAgICBwdWJsaWMgRGljdGlvbmFyeSgpIHsKICAgICAgICB0YWJsZSA9IG5ldyBPYmplY3RbdGFibGVTaXplXTsKICAgIH0KCiAgICBwdWJsaWMgdm9pZCBpbnNlcnQoSyBrZXksIFYgdmFsdWUpIHsKICAgICAgICBpbnQgaGFzaCA9IGtleS5oYXNoQ29kZSgpOwogICAgICAgIGhhc2ggPSAoaGFzaCAmIDB4N2ZmZmZmZmYpICUgdGFibGVTaXplOwogICAgICAgIGlmKHRhYmxlW2hhc2hdICE9IG51bGwpIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJ1ZHBhdGUg8J+YgiIpOwogICAgICAgICAgICB0YWJsZVtoYXNoXSA9IG5ldyBFbnRyeShrZXksIHZhbHVlKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkluc2VydGVkIPCfmIEiKTsKICAgICAgICAgICAgdGFibGVbaGFzaF0gPSBuZXcgRW50cnkoa2V5LCB2YWx1ZSk7CiAgICAgICAgICAgIHNpemUgKz0gMTsKICAgICAgICB9CiAgICAgICAgLy8gU3lzdGVtLm91dC5wcmludGxuKGhhc2gpOwogICAgICAgIAogICAgfQogICAgCiAgICBwdWJsaWMgViBnZXQoSyBrZXkpIHsKICAgICAgICBpbnQgaGFzaCA9IGtleS5oYXNoQ29kZSgpOwogICAgIC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