diff --git a/src/main/java/basic/datastructure/map/AbstractMap.java b/src/main/java/basic/datastructure/map/AbstractMap.java new file mode 100644 index 0000000..e217088 --- /dev/null +++ b/src/main/java/basic/datastructure/map/AbstractMap.java @@ -0,0 +1,9 @@ +package basic.datastructure.map; + +public abstract class AbstractMap implements Map{ + + + protected void checkNullKey(K key){ + if(key==null) throw new NullPointerException(); + } +} diff --git a/src/main/java/basic/datastructure/map/ListMap.java b/src/main/java/basic/datastructure/map/ListMap.java new file mode 100644 index 0000000..94d5edc --- /dev/null +++ b/src/main/java/basic/datastructure/map/ListMap.java @@ -0,0 +1,106 @@ +package basic.datastructure.map; + +import basic.datastructure.list.ArrayList; + +public class ListMap extends AbstractMap { + + ArrayList list; + + public ListMap(){ + list = new ArrayList<>(); + } + + private int findIndexByKey(K key){ + checkNullKey(key); + for(int i =0 ;i(); + } + + @Override + public int size() { + return list.getSize(); + } + + @Override + public boolean isEmpty() { + return list.getSize()==0; + } + + private class Node implements Map.Entry{ + + private K key; + + private V value; + + Node(K key, V value){ + this.key = key; + + this.value = value; + } + + @Override + public K getKey() { + return this.key; + } + + @Override + public V getValue() { + return this.value; + } + + @Override + public V setValue(V value) { + this.value = value; + return this.value; + } + } +} diff --git a/src/main/java/basic/datastructure/map/Map.java b/src/main/java/basic/datastructure/map/Map.java new file mode 100644 index 0000000..881733d --- /dev/null +++ b/src/main/java/basic/datastructure/map/Map.java @@ -0,0 +1,25 @@ +package basic.datastructure.map; + +public interface Map { + + void put(K key, V value); + + boolean containsKey(K key); + + V get(K key); + + V remove(K key); + + void clear(); + + int size(); + + boolean isEmpty(); + + + interface Entry { + K getKey(); + V getValue(); + V setValue(V value); + } +}