package ru.job4j.GenericDirectory;


import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Created by Comp on 04.11.2017.
 */

public class GenericDirectory<K, V> implements SimpleMap<K, V> {
    private Entry[] entry;
    private Entry store;
    private int size = 0;
    private int counter = 0;
    private static final int ENTRY_SIZE = 100;

    public GenericDirectory() {
        this.entry = new Entry[ENTRY_SIZE];
    }

    private int indexOf(K key) {
        return key.hashCode() % entry.length;
    }

    /**
     * Insert new key-value pair at the storage.
     *
     * @param key   for adding. Notice that is must be unique, otherwise you may loose old value.
     * @param value for adding.
     * @return true if pair was added, otherwise false.
     */
    public boolean insert(K key, V value) {
        boolean isInsert;

        if (value == null) {
            isInsert = false;
        }

        int index = indexOf(key);
        if (!contains(key)) {
            entry[index] = new Entry<K, V>(key, value);
            isInsert = true;
        } else {
            isInsert = false;
        }

        return isInsert;
    }

    /**
     * Return element from dictionary.
     *
     * @param key unique object for each value in the dictionary.
     * @return if is find at the dictionary return object from dictionary, otherwise false.
     */
    public V get(K key) {
        int index = indexOf(key);

        if (contains(key) ) {
            return (V) entry[index];

        } else {
            return null;
        }

    }


    /**
     * Check that given key exist dictionary and delete element.
     *
     * @param key object for checking.
     * @return true if object delete at the set.
     */
    public boolean delete(K key) {
        boolean isDelete = false;

        int index = indexOf(key);

        if (contains(key)) {
            entry[index] = null;
            isDelete = true;
        }

        return isDelete;
    }

    private boolean contains(K key) {
        return entry[indexOf(key)] != null;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        GenericDirectory<?, ?> directory = (GenericDirectory<?, ?>) o;

        if (size != directory.size) return false;
        // Probably incorrect - comparing Object[] arrays with Arrays.equals
        if (!Arrays.equals(entry, directory.entry)) return false;
        return store != null ? store.equals(directory.store) : directory.store == null;
    }

    @Override
    public int hashCode() {
        int result = Arrays.hashCode(entry);
        result = 31 * result + (store != null ? store.hashCode() : 0);
        result = 31 * result + size;
        return result;
    }

    public int size() {
        return this.size;
    }

    @Override
    public Iterator iterator() {
        return new Iterat();
    }

    private class Iterat implements Iterator {
        @Override
        public boolean hasNext() {
            boolean isPresent;
            return isPresent = size() != counter;
        }

        @Override
        public Object next() {
            if (hasNext()) {
                return entry[counter++];
            } else throw new NoSuchElementException();
        }
    }
}

@@ -34,33 +34,8 @@
 ;%0A%0A%0A
-import java.util.Arrays;%0A
 impo
@@ -57,16 +57,16 @@
 erator;%0A
+
 import j
@@ -238,33 +238,8 @@
 ry;%0A
-    private Entry store;%0A
     
@@ -1102,32 +1102,48 @@
 false;%0A        %7D
+%0A        size++;
 %0A%0A        return
@@ -1508,17 +1508,16 @@
 index%5D;%0A
-%0A
         
@@ -1564,17 +1564,15 @@
   %7D%0A
-%0A
     %7D%0A%0A
-%0A
     
@@ -1941,33 +1941,48 @@
 true;%0A        %7D%0A
+        size--;
 %0A
-
         return i
@@ -2027,32 +2027,32 @@
 ntains(K key) %7B%0A
+
         return e
@@ -2090,749 +2090,8 @@
  %7D%0A%0A
-    @Override%0A    public boolean equals(Object o) %7B%0A        if (this == o) return true;%0A        if (o == null %7C%7C getClass() != o.getClass()) return false;%0A%0A        GenericDirectory%3C?, ?%3E directory = (GenericDirectory%3C?, ?%3E) o;%0A%0A        if (size != directory.size) return false;%0A        // Probably incorrect - comparing Object%5B%5D arrays with Arrays.equals%0A        if (!Arrays.equals(entry, directory.entry)) return false;%0A        return store != null ? store.equals(directory.store) : directory.store == null;%0A    %7D%0A%0A    @Override%0A    public int hashCode() %7B%0A        int result = Arrays.hashCode(entry);%0A        result = 31 * result + (store != null ? store.hashCode() : 0);%0A        result = 31 * result + size;%0A        return result;%0A    %7D%0A%0A
     

Update Создать элементарную структуру дерева[#26347]