> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/exercicios-java/ud6-solucions/mapas.md).

# Mapas

```java
public class EntradaMapa<K, V> {

    private K chave;
    private V valor;

    public EntradaMapa(K chave, V valor) {
        this.chave = chave;
        this.valor = valor;
    }

    public K getChave() {
        return chave;
    }

    public void setChave(K chave) {
        this.chave = chave;
    }

    public V getValor() {
        return valor;
    }

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

    @Override
    public String toString() {
        return chave + " = " + valor;
    }
}
```

```java
public class Mapa<K, V> {

    private EntradaMapa<K, V>[] entradas;

    public Mapa() {
        entradas = new EntradaMapa[10];
    }

    private int getIndice(K chave) {
        return Math.abs(chave.hashCode() % CAPACIDADE);
    }

    public void put(K chave, V valor) {
        int indice = getIndice(chave);
        EntradaMapa<K, V> novaEntrada = new EntradaMapa<>(chave, valor);

        if (entradas[indice] == null) {
            entradas[indice] = novaEntrada;
        } else {
            if (entradas[indice].getChave().equals(chave)) {
                entradas[indice].setValor(valor);
            } else {
                System.out.println("Non foi posible a inserción, " +
                        "houbo unha colisión na posición " + indice);
            }
        }
    }

    public V get(K chave) {
        int indice = getIndice(chave);
        EntradaMapa<K, V> entrada = entradas[indice];

        if (entrada != null && entrada.getChave().equals(chave)) {
            return entrada.getValor();
        }
        return null;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("Mapa {\n");
        for (EntradaMapa<K, V> entrada : entradas) {
            if (entrada != null) {
                sb.append("  [").append(entrada).append("]\n");
            }
        }
        sb.append("}");
        return sb.toString();
    }
}
```

```java
public class App {

    public static void main(String[] args) {

        Mapa<String, Integer> mapa = new Mapa<>();

        // Insercións normais
        mapa.put("casa", 15);
        mapa.put("can", 4);
        mapa.put("gato", 7);
        mapa.put("paxaro", 2);
        System.out.println("Após insercións iniciais:");
        System.out.println(mapa);

        // Recuperación de valores
        System.out.println("Valor de 'casa': " + mapa.get("casa"));
        System.out.println("Valor de 'can':  " + mapa.get("can"));
        System.out.println("Valor de 'rato' (non existe): " + mapa.get("rato"));

        // Actualización dun valor existente
        System.out.println("\nActualizando 'casa' a 99...");
        mapa.put("casa", 99);
        System.out.println("Valor de 'casa' tras actualizar: " + mapa.get("casa"));

        // Forza unha colisión manual: buscamos dúas chaves co mesmo índice
        // hashCode("Aa") == hashCode("BB") en Java → mesma posición no array
        System.out.println("\nProbando colisión:");
        mapa.put("Aa", 100);
        mapa.put("BB", 200); // colisión con "Aa"
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://educacion.gitbook.io/programacion/exercicios-java/ud6-solucions/mapas.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
