Java HashMap Implementation: Code and Explanation

Java HashMap Implementation

This document provides a detailed implementation of a HashMap in Java.

public class HashMap<K, V> implements Map<K, V> {
    private int capacidad;
    private int numElem;
    private Lista<Map.Par<K, V>>[] arrayMapa;

    public HashMap(int cap) throws IllegalArgumentException {
        if (cap < 0) {
            throw new IllegalArgumentException("NUMERO NEGATIVO");
        } else {
            this.capacidad = cap;
            this.numElem = 0;
            this.arrayMapa = new Lista[capacidad];
            for (int i = 0; i < capacidad; i++) {
                arrayMapa[i] = new ListaEnlazada<>();
            }
        }
    }

    public HashMap() {
        this(15);
    }

    private int funcionHash(K clave) {
        return Math.abs(clave.hashCode() % capacidad);
    }

    public int tamaƱo() {
        return this.numElem;
    }

    public V get(K clave) {
        int indice = funcionHash(clave);
        for (Map.Par<K, V> puntero : arrayMapa[indice]) {
            if (puntero.getClave().equals(clave)) {
                return puntero.getValor();
            }
        }
        return null;
    }

    public void insertar(K clave, V valor) {
        V v2 = this.get(clave);
        int indice = funcionHash(clave);
        if (v2 != null) {
            for (Map.Par<K, V> puntero : arrayMapa[indice]) {
                if (puntero.getClave().equals(clave)) {
                    puntero.setValor(valor);
                }
            }
        } else {
            arrayMapa[indice].insertarFinal(new Combo<>(clave, valor));
            this.numElem++;
        }
    }

    @Override
    public V eliminar(K clave) {
        int indice = funcionHash(clave);
        V v2 = this.get(clave);
        if (v2 != null && arrayMapa[indice].contiene(new Combo<>(clave, v2))) {
            arrayMapa[indice].suprimir(new Combo<>(clave, v2));
            this.numElem--;
            return v2;
        } else {
            return null;
        }
    }

    @Override
    public Iterator<K> getClaves() {
        Lista<K> lista = new ListaEnlazada<>();
        for (int i = 0; i < this.capacidad; i++) {
            for (Map.Par<K, V> puntero : arrayMapa[i]) {
                lista.insertarFinal(puntero.getClave());
            }
        }
        return lista.iterator();
    }

    @Override
    public Iterator<V> getValores() {
        Lista<V> lista = new ListaEnlazada<>();
        for (int i = 0; i < this.capacidad; i++) {
            for (Map.Par<K, V> puntero : arrayMapa[i]) {
                lista.insertarPrincipio(puntero.getValor());
            }
        }
        return lista.iteradorLista();
    }

    static class Combo<K, V> implements Par<K, V> {
        private K clave;
        private V valor;

        public Combo(K clave, V valor) {
            this.clave = clave;
            this.valor = valor;
        }

        @Override
        public K getClave() {
            return this.clave;
        }

        @Override
        public V getValor() {
            return this.valor;
        }

        @Override
        public void setValor(V valorN) {
            this.valor = valorN;
        }
    }
}