map

map


package homework2;

import java.util.Map;

public class HashMap<K, V> implements MapDS<K, V> {

        private int capacity = 100;
        private int size = 0;

        private LinkedList<Node<K, V>> data[];

        class Node<K, V> {
            K key;
            V value;
            Node<K,V> nextNode;

            protected Node(K key, V value) {
                this.key = key;
                this.value = value;
            }

            public K getKey(){
                return key;
            }

            public V getValue(){
                return value;
            }

            public void setValue(V value) {
                this.value = value;
            }
        }

        public HashMap() {
            data = new LinkedList[capacity];
        }

        private int hash(int hashCode){
            return Math.abs(hashCode) % 100;

        }

        /**
         * Removes all of the mappings from this map.
         */
        public void clear(){
            size = 0;
            for (LinkedList x : data) {
                x.clear();
            }
        }

        /**
         * Returns true if this map contains a mapping for the specified key.
         * @param key
         * @return
         */
        public boolean containsKey(K key){
            if (get(key) != null)
                return true;
            return false;
        }

        /**
         * Returns true if this map maps one or more keys to the specified value.
         * @param value
         * @return
         */
        public boolean containsValue(V value){
            for (int i = 0; i < capacity; i++) {
                LinkedList<Node<K, V>> currentBin = data[i];
                for (int j = 0; j < currentBin.size(); j++) {
                    Node<K, V> currentNode = currentBin.get(j);
                    if (value.equals(currentBin.get(j).getValue()))
                        return true;
                }
            }
            return false;
        }

        /**
         * Returns the value to which the specified key is mapped, or null if this
         * map contains no mapping for the key.
         * @param key
         * @return
         */
        public V   get(Object key){
            V myValue = null;
            LinkedList<Node<K, V>> matchedBin = data[hash(key.hashCode())];
            for (int i = 0; i < matchedBin.size(); i++) {
                Node<K, V> currentNode = matchedBin.get(i);
                if (key.equals(currentNode.getKey()))
                    myValue = currentNode.getValue();
            }
            return myValue;
        }
        /**
         * Returns true if this map contains no key-value mappings.
         * @return
         */
        public boolean isEmpty(){
            for (int i = 0; i < size; i++) {
                LinkedList<Node<K, V>> currentBin = data[i];
                if (!currentBin.isEmpty())
                    return false;
            }
            return true;
        }

        /**
         * Associates the specified value with the specified key in this map.
         * @param key
         * @param value
         * @return
         */
        public V   put(K key, V value){
            Node<K, V> newNode = new Node<>(key, value);
            int newIndex = hash(key.hashCode());
            if (data[newIndex] == null) {
                data[newIndex] = new LinkedList<>();
            }
            data[newIndex].add(newNode);
            size++;
            return value;
        }

        /**
         * Copies all of the mappings from the specified map to this map.
         * @param m
         */
        public void    putAll(Map<? extends K,? extends V> m){
                m.forEach(this::put);
        }

        /**
         * Removes the mapping for a key from this map if it is present (optional operation).
         * @param key
         * @return
         */
        public V   remove(Object key){
            if (containsKey((K)key)){
                LinkedList<Node<K, V>> matchedBin = data[hash(key.hashCode())];
                for (int i = 0; i < matchedBin.size(); i++) {
                    Node<K, V> currentNode = matchedBin.get(i);
                    if (key.equals(currentNode.getKey())) {
                        V removedValue = currentNode.getValue();
                        matchedBin.remove(currentNode);
                        size--;
                        return removedValue;
                    }
                }
            }
            return null;
        }

        /**
         * Removes the entry for the specified key only if it is currently mapped to the
         * specified value.
         * @param key
         * @param value
         * @return
         */
        public boolean remove(Object key, Object value){
            if (containsKey((K)key)){
                LinkedList<Node<K, V>> matchedBin = data[hash(key.hashCode())];
                for (int i = 0; i < matchedBin.size(); i++) {
                    Node<K, V> currentNode = matchedBin.get(i);
                    if (key.equals(currentNode.getKey()) && value.equals(currentNode.getValue())) {
                        matchedBin.remove(currentNode);
                        size--;
                        return true;
                    }
                }
            }
            return false;
        }

        /**
         * Replaces the entry for the specified key only if it is currently mapped to some value.
         * @param key
         * @param value
         * @return
         */
        public V replace(K key, V value){
            if (containsKey((K)key)){
                LinkedList<Node<K, V>> matchedBin = data[hash(key.hashCode())];
                for (int i = 0; i < matchedBin.size(); i++) {
                    Node<K, V> currentNode = matchedBin.get(i);
                    if (key.equals(currentNode.getKey()) && currentNode.getValue() != null) {
                        currentNode.setValue(value);
                        return value;
                    }
                }
            }
            return value;
        }

        /**
         * Replaces the entry for the specified key only if currently mapped to the
         * @param key
         * @param oldValue
         * @param newValue
         * @return
         */
        public boolean replace(K key, V oldValue, V newValue){
            if (containsKey((K)key)){
                LinkedList<Node<K, V>> matchedBin = data[hash(key.hashCode())];
                for (int i = 0; i < matchedBin.size(); i++) {
                    Node<K, V> currentNode = matchedBin.get(i);
                    if (key.equals(currentNode.getKey()) && oldValue.equals(currentNode.getValue())) {
                        currentNode.setValue(newValue);
                        return true;
                    }
                }
            }
            return false;
        }

        /**
         * Returns the number of key-value mappings in this map.
         * @return
         */
        public int size(){
            return size;
        }


}


Report Page