public interface YouthMap<K,V> {
int size();
V put(K key,V val);
V get(K key);
interface Entry<K,V>{
K getKey();
V getValue();
V setValue(V value);
}
}
public class LinkedListMap<K,V> implements YouthMap<K,V> {
private Object[] objects = new Object[100];
private LinkedList<Entry>[] entrys=new LinkedList[100];
@Override
public int size() {
return entrys.length;
}
@Override
public V put(K key, V val) {
int hash = hash(key);
LinkedList<Entry> linkedList = entrys[hash];
Entry entry = new Entry(key, val);
if (linkedList == null) {
linkedList = new LinkedList();
linkedList.add(entry);
entrys[hash] = linkedList;
return val;
}
for (Entry ety : linkedList) {
if (ety.getKey().equals(key)) {
ety.setValue(val);
return val;
}
}
linkedList.add(entry);
entrys[hash] = linkedList;
return val;
}
private int hash(K key) {
int hashCode = key.hashCode();
int hash = hashCode % objects.length;
return hash;
}
@Override
public V get(K key) {
int hash = hash(key);
LinkedList<Entry> linkedList = entrys[hash];
for (Entry ety : linkedList) {
if (ety.getKey().equals(key)) {
return (V) ety.getValue();
}
}
return null;
}
class Entry<K, V> implements YouthMap.Entry<K, V> {
private K k;
private V v;
public K getKey() {
return this.k;
}
public V getValue() {
return this.v;
}
public V setValue(V value) {
this.v = v;
return this.v;
}
public Entry(K k, V v) {
this.k = k;
this.v = v;
}
}
}
public class Test {
public static void main(String[] args) {
LinkedListMap<String, String> myMap = new LinkedListMap<String, String>();
myMap.put("key", "value");
System.out.println(myMap.get("key"));
}
}