> 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/conxuntos.md).

# Conxuntos

```java
package EstructurasDeDatos.Conjuntos;

import java.util.Arrays;

public class Conjunto {

    private Integer[] valores;

    public Conjunto(Integer[] val){
        this.valores = val;
    }

    private int calcularPosicion(Integer elemento){
        return Math.abs(elemento.hashCode())%this.valores.length;
    }

    public boolean contains(Integer elemento){
        if (elemento != null) {
            Integer acceso = valores[this.calcularPosicion(elemento)];
            if (acceso!=null)
                return acceso.equals(elemento);
        }
        return false;
    }

    public void add(Integer elemento) {
        if (elemento == null) return;
        int posicion = this.calcularPosicion(elemento);
        if (valores[posicion]==null) {
            valores[posicion] = elemento;
        } else {
            System.out.println("Posicion: " + posicion + "ocupada");
        }
    }

    public Conjunto interseccion(Conjunto interseccion){
        int tam = Math.min(interseccion.valores.length, this.valores.length);
        Conjunto nuevo = new Conjunto(new Integer[tam]);
        for (Integer i: this.valores){
            if (i!=null && interseccion.contains(i))
                nuevo.add(i);
        }
        return nuevo;
    }

    public Conjunto union(Conjunto union){
        int tam = union.valores.length + this.valores.length;
        Conjunto nuevo = new Conjunto(new Integer[tam]);
        for (Integer i: this.valores){
            if (i!=null)
                nuevo.add(i);
        }
        for (Integer i: union.valores){
            if (i!=null)
                nuevo.add(i);
        }
        return nuevo;
    }

    @Override
    public String toString() {
        return "Conjunto{" +
                "valores=" + Arrays.toString(valores) +
                '}';
    }

}

```

```java
package EstructurasDeDatos.Conjuntos;

public class App {

    public static void main(String[] args) {
        Conjunto c1 = new Conjunto(new Integer[10]);
        Conjunto c2 = new Conjunto(new Integer[10]);

        c1.add(10); // Posición 0 (10 % 10 = 0)
        c1.add(25); // Posición 5
        c1.add(3);  // Posición 3
        c1.add(17); // Posición 7

        System.out.println(c1);

        c2.add(10); // Común
        c2.add(8);  // Diferente
        c2.add(3);  // Común

        System.out.println(c2);

        Conjunto inter = c1.interseccion(c2);
        System.out.println("Interseccion:" + inter);
        Conjunto union = c1.union(c2);
        System.out.println("Union; " + union);

    }
}
```


---

# 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/conxuntos.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.
