> 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-set-e-hashset.md).

# Conxuntos: Set e HashSet

```java
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class GrupoAlumnos {

    private Set<String> alumnos;

    public GrupoAlumnos(List<String> alumnos) {
        this.alumnos = new HashSet<>(alumnos);
    }

    private GrupoAlumnos(Set<String> alumnos) {
        this.alumnos = alumnos;
    }

    public void engadir(String alumno) {
        alumnos.add(alumno);
    }

    public void eliminar(String alumno) {
        alumnos.remove(alumno);
    }

    public boolean contains(String alumno) {
        return alumnos.contains(alumno);
    }

    public GrupoAlumnos union(GrupoAlumnos outro) {
        Set<String> resultado = new HashSet<>(this.alumnos);
        resultado.addAll(outro.alumnos);
        return new GrupoAlumnos(resultado);
    }

    public GrupoAlumnos interseccion(GrupoAlumnos outro) {
        Set<String> resultado = new HashSet<>(this.alumnos);
        resultado.retainAll(outro.alumnos);
        return new GrupoAlumnos(resultado);
    }

    @Override
    public String toString() {
        return "GrupoAlumnos{" + "alumnos=" + alumnos + '}';
    }
}
```

```java
import java.util.Arrays;
import java.util.List;

public class App {
    public static void main(String[] args) {

        List<String> listaA = Arrays.asList("Ana", "Carlos", "Marta", "Xoan");
        List<String> listaB = Arrays.asList("María", "Jorge", "Lucía", "David");

        GrupoAlumnos grupoA = new GrupoAlumnos(listaA);
        GrupoAlumnos grupoB = new GrupoAlumnos(listaB);

        // Engadir
        grupoA.engadir("Luís");
        grupoA.engadir("Ana"); // Duplicado, non se engade
        System.out.println("GrupoA: " + grupoA);

        // Eliminar
        grupoA.eliminar("Carlos");
        System.out.println("GrupoA sen Carlos: " + grupoA);

        // Contains
        System.out.println("Marta está en grupoA: " + grupoA.contains("Marta"));
        System.out.println("Carlos está en grupoA: " + grupoA.contains("Carlos"));

        // Unión
        GrupoAlumnos union = grupoA.union(grupoB);
        System.out.println("Unión: " + union);

        // Intersección (grupos sen alumnos comúns)
        GrupoAlumnos interseccion = grupoA.interseccion(grupoB);
        System.out.println("Intersección: " + interseccion);
    }
}
```


---

# 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-set-e-hashset.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.
