1
+ import java .util .*;
2
+
3
+ public class eulogioep {
4
+ public static void main (String [] args ) {
5
+ // TEORÍA: Los conjuntos en Java
6
+ /*
7
+ * En Java, un Set es una colección que no puede contener elementos duplicados.
8
+ * Hay tres implementaciones principales de Set:
9
+ * 1. HashSet - La más rápida, no mantiene orden
10
+ * 2. TreeSet - Mantiene los elementos ordenados
11
+ * 3. LinkedHashSet - Mantiene el orden de inserción
12
+ *
13
+ * Para este ejercicio, usaremos principalmente HashSet para las operaciones básicas
14
+ * y luego mostraremos ejemplos con otros tipos para las operaciones adicionales.
15
+ */
16
+
17
+ // PARTE 1: OPERACIONES BÁSICAS CON CONJUNTOS
18
+ System .out .println ("PARTE 1: OPERACIONES BÁSICAS" );
19
+
20
+ // Creación de un conjunto
21
+ Set <String > conjunto = new HashSet <>();
22
+ System .out .println ("Conjunto inicial: " + conjunto );
23
+
24
+ // 1. Añadir un elemento al final
25
+ // NOTA: En un HashSet no hay "final" como tal, ya que no mantiene orden
26
+ conjunto .add ("Elemento1" );
27
+ System .out .println ("Después de añadir un elemento: " + conjunto );
28
+
29
+ // 2. Añadir un elemento al principio
30
+ // NOTA: Como HashSet no mantiene orden, es igual que añadir al final
31
+ conjunto .add ("Elemento2" );
32
+ System .out .println ("Después de añadir otro elemento: " + conjunto );
33
+
34
+ // 3. Añadir varios elementos en bloque al final
35
+ List <String > elementosNuevos = Arrays .asList ("Elemento3" , "Elemento4" , "Elemento5" );
36
+ conjunto .addAll (elementosNuevos );
37
+ System .out .println ("Después de añadir varios elementos: " + conjunto );
38
+
39
+ // 4. Añadir elementos en una posición concreta
40
+ // NOTA: HashSet no permite insertar en posiciones específicas
41
+ // Para demostrar esto, usaremos una LinkedList temporalmente
42
+ List <String > listaOrdenada = new LinkedList <>(conjunto );
43
+ listaOrdenada .addAll (2 , Arrays .asList ("ElementoA" , "ElementoB" ));
44
+ conjunto = new HashSet <>(listaOrdenada );
45
+ System .out .println ("Después de 'insertar' en posición específica: " + conjunto );
46
+
47
+ // 5. Eliminar un elemento
48
+ conjunto .remove ("Elemento3" );
49
+ System .out .println ("Después de eliminar 'Elemento3': " + conjunto );
50
+
51
+ // 6. Actualizar un elemento
52
+ // NOTA: En un Set no se pueden actualizar elementos directamente
53
+ // Hay que eliminar el viejo y añadir el nuevo
54
+ if (conjunto .remove ("Elemento4" )) {
55
+ conjunto .add ("Elemento4Actualizado" );
56
+ }
57
+ System .out .println ("Después de actualizar 'Elemento4': " + conjunto );
58
+
59
+ // 7. Comprobar si un elemento está en el conjunto
60
+ boolean contiene = conjunto .contains ("Elemento1" );
61
+ System .out .println ("¿Contiene 'Elemento1'? " + contiene );
62
+
63
+ // 8. Eliminar todo el contenido
64
+ conjunto .clear ();
65
+ System .out .println ("Después de limpiar el conjunto: " + conjunto );
66
+
67
+ // PARTE 2: OPERACIONES EXTRA CON CONJUNTOS
68
+ System .out .println ("\n PARTE 2: OPERACIONES EXTRA" );
69
+
70
+ Set <Integer > conjunto1 = new HashSet <>(Arrays .asList (1 , 2 , 3 , 4 , 5 ));
71
+ Set <Integer > conjunto2 = new HashSet <>(Arrays .asList (4 , 5 , 6 , 7 , 8 ));
72
+
73
+ // 1. Unión
74
+ Set <Integer > union = new HashSet <>(conjunto1 );
75
+ union .addAll (conjunto2 );
76
+ System .out .println ("Unión: " + union );
77
+
78
+ // 2. Intersección
79
+ Set <Integer > interseccion = new HashSet <>(conjunto1 );
80
+ interseccion .retainAll (conjunto2 );
81
+ System .out .println ("Intersección: " + interseccion );
82
+
83
+ // 3. Diferencia
84
+ Set <Integer > diferencia = new HashSet <>(conjunto1 );
85
+ diferencia .removeAll (conjunto2 );
86
+ System .out .println ("Diferencia (conjunto1 - conjunto2): " + diferencia );
87
+
88
+ // 4. Diferencia simétrica
89
+ Set <Integer > diferenciaSimetrica = new HashSet <>(conjunto1 );
90
+ diferenciaSimetrica .addAll (conjunto2 ); // Unión
91
+ Set <Integer > temp = new HashSet <>(conjunto1 );
92
+ temp .retainAll (conjunto2 ); // Intersección
93
+ diferenciaSimetrica .removeAll (temp ); // Unión - Intersección
94
+ System .out .println ("Diferencia simétrica: " + diferenciaSimetrica );
95
+ }
96
+ }
0 commit comments