Java : Set con ejemplos
Set (Java SE 23 & JDK 23) en Java con ejemplos.
Encontrará muestras de código para la mayoría de los métodos Set<E>.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
Una colección que no contiene elementos duplicados. Más formalmente, los conjuntos no contienen ningún par de elementos e1 y e2 tales que e1.equals(e2), y como máximo un elemento nulo. Como lo implica su nombre, esta interfaz modela la abstracción matemática de conjuntos. (Traducción automática)
final Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3
System.out.println(set.contains("a")); // true
System.out.println(set.contains("b")); // true
System.out.println(set.contains("X")); // false
Methods
boolean add (E e)
Agrega el elemento especificado a este conjunto si aún no está presente (operación opcional). (Traducción automática)
final Set<String> set = new HashSet<>();
System.out.println(set); // []
System.out.println(set.add("a")); // true
System.out.println(set); // [a]
System.out.println(set.add("b")); // true
System.out.println(set); // [a, b]
System.out.println(set.add("c")); // true
System.out.println(set); // [a, b, c]
System.out.println(set.add("a")); // false
System.out.println(set); // [a, b, c]
boolean addAll (Collection<? extends E> c)
Agrega todos los elementos de la colección especificada a este conjunto si aún no están presentes (operación opcional). (Traducción automática)
final var src = Set.of("a", "b", "c");
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.addAll(Set.of("d", "e", "f"))); // true
System.out.println(set); // [a, b, c, d, e, f]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.addAll(Set.of("a", "b", "c"))); // false
System.out.println(set); // [a, b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.addAll(Set.of("a", "c", "e", "f"))); // true
System.out.println(set); // [a, b, c, e, f]
}
void clear ()
Elimina todos los elementos de este conjunto (operación opcional). (Traducción automática)
final Set<String> set = new HashSet<>();
System.out.println(set); // []
set.add("a");
System.out.println(set); // [a]
set.add("b");
System.out.println(set); // [a, b]
set.clear();
System.out.println(set); // []
boolean contains (Object o)
Devuelve verdadero si este conjunto contiene el elemento especificado. (Traducción automática)
final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]
System.out.println(set.contains("")); // false
System.out.println(set.contains("a")); // true
System.out.println(set.contains("b")); // true
System.out.println(set.contains("c")); // true
System.out.println(set.contains("X")); // false
System.out.println(set.contains("abc")); // false
boolean containsAll (Collection<?> c)
Devuelve verdadero si este conjunto contiene todos los elementos de la colección especificada. (Traducción automática)
final var set = Set.of("a", "b", "c");
System.out.println(set); // [c, b, a]
System.out.println(set.containsAll(Set.<String>of())); // true
System.out.println(set.containsAll(Set.of("a"))); // true
System.out.println(set.containsAll(Set.of("a", "b"))); // true
System.out.println(set.containsAll(Set.of("b", "a"))); // true
System.out.println(set.containsAll(Set.of("c", "b", "a"))); // true
System.out.println(set.containsAll(Set.of("X"))); // false
System.out.println(set.containsAll(Set.of("a", "X"))); // false
System.out.println(set.containsAll(Set.of("a", "b", "c", "X"))); // false
static <E> Set<E> copyOf (Collection<? extends E> coll)
Devuelve un conjunto no modificable que contiene los elementos de la colección dada. (Traducción automática)
final Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
final var copiedSet = Set.copyOf(set);
System.out.println(copiedSet); // [a, b, c]
set.add("d");
System.out.println(set); // [a, b, c, d]
System.out.println(copiedSet); // [a, b, c]
try {
copiedSet.add("d");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
boolean equals (Object o)
Compara el objeto especificado con este conjunto para verificar la igualdad. (Traducción automática)
final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]
System.out.println(set.equals(Set.of("a"))); // false
System.out.println(set.equals(Set.of("a", "b"))); // false
System.out.println(set.equals(Set.of("a", "b", "c"))); // true
System.out.println(set.equals(Set.of("c", "b", "a"))); // true
final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]
final Set<String> hashSet = new HashSet<>(set);
System.out.println(hashSet); // [a, b, c]
System.out.println(set.equals(hashSet)); // true
final Set<String> treeSet = new TreeSet<>(set);
System.out.println(treeSet); // [a, b, c]
System.out.println(set.equals(treeSet)); // true
final var list = new ArrayList<>(set);
System.out.println(list); // [a, b, c]
System.out.println(set.equals(list)); // false
int hashCode ()
Devuelve el valor del código hash para este conjunto. (Traducción automática)
final var set1 = Set.of("a", "b", "c");
System.out.println(set1.hashCode()); // 294
final var set2 = Set.of(1, 2, 3);
System.out.println(set2.hashCode()); // 6
final Set<String> set1 = new LinkedHashSet<>();
set1.add("a");
set1.add("b");
set1.add("c");
System.out.println(set1); // [a, b, c]
System.out.println(set1.hashCode()); // 294
final Set<String> set2 = new LinkedHashSet<>();
set2.add("c");
set2.add("b");
set2.add("a");
System.out.println(set2); // [c, b, a]
System.out.println(set2.hashCode()); // 294
boolean isEmpty ()
Devuelve verdadero si este conjunto no contiene elementos. (Traducción automática)
final Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.isEmpty()); // false
set.clear();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true
Iterator<E> iterator ()
Devuelve un iterador sobre los elementos de este conjunto. (Traducción automática)
final var set = Set.of("aaa", "bbb", "ccc");
System.out.println(set); // [aaa, ccc, bbb]
final var it = set.iterator();
System.out.println("-- forEachRemaining --");
it.forEachRemaining(System.out::println);
// Result
// ↓
//-- forEachRemaining --
//aaa
//ccc
//bbb
static <E> Set<E> of ()
Devuelve un conjunto no modificable que contiene cero elementos. (Traducción automática)
final var set = Set.of();
System.out.println(set); // []
System.out.println(set.size()); // 0
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1)
Devuelve un conjunto no modificable que contiene un elemento. (Traducción automática)
final var set = Set.of("a");
System.out.println(set); // [a]
System.out.println(set.size()); // 1
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E... elements)
Devuelve un conjunto no modificable que contiene un número arbitrario de elementos. (Traducción automática)
final var set = Set.of(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n"
);
System.out.println(set); // [c, d, e, f, g, h, i, j, k, l, m, n, a, b]
System.out.println(set.size()); // 14
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2)
Devuelve un conjunto no modificable que contiene dos elementos. (Traducción automática)
final var set = Set.of("a", "b");
System.out.println(set); // [b, a]
System.out.println(set.size()); // 2
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3)
Devuelve un conjunto no modificable que contiene tres elementos. (Traducción automática)
final var set = Set.of("a", "b", "c");
System.out.println(set); // [b, a, c]
System.out.println(set.size()); // 3
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3, E e4)
Devuelve un conjunto no modificable que contiene cuatro elementos. (Traducción automática)
final var set = Set.of("a", "b", "c", "d");
System.out.println(set); // [b, a, d, c]
System.out.println(set.size()); // 4
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5)
Devuelve un conjunto no modificable que contiene cinco elementos. (Traducción automática)
final var set = Set.of("a", "b", "c", "d", "e");
System.out.println(set); // [e, d, c, b, a]
System.out.println(set.size()); // 5
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5, E e6)
Devuelve un conjunto no modificable que contiene seis elementos. (Traducción automática)
final var set = Set.of("a", "b", "c", "d", "e", "f");
System.out.println(set); // [f, e, d, c, b, a]
System.out.println(set.size()); // 6
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7)
Devuelve un conjunto no modificable que contiene siete elementos. (Traducción automática)
final var set = Set.of("a", "b", "c", "d", "e", "f", "g");
System.out.println(set); // [g, f, e, d, c, b, a]
System.out.println(set.size()); // 7
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
Devuelve un conjunto no modificable que contiene ocho elementos. (Traducción automática)
final var set = Set.of("a", "b", "c", "d", "e", "f", "g", "h");
System.out.println(set); // [h, g, f, e, d, c, b, a]
System.out.println(set.size()); // 8
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
Devuelve un conjunto no modificable que contiene nueve elementos. (Traducción automática)
final var set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i");
System.out.println(set); // [a, i, h, g, f, e, d, c, b]
System.out.println(set.size()); // 9
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10)
Devuelve un conjunto no modificable que contiene diez elementos. (Traducción automática)
final var set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
System.out.println(set); // [j, i, h, g, f, e, d, c, b, a]
System.out.println(set.size()); // 10
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
boolean remove (Object o)
Elimina el elemento especificado de este conjunto si está presente (operación opcional). (Traducción automática)
final var src = Set.of("a", "b", "c");
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("")); // false
System.out.println(set); // [a, b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("a")); // true
System.out.println(set); // [b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("b")); // true
System.out.println(set); // [a, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("X")); // false
System.out.println(set); // [a, b, c]
}
boolean removeAll (Collection<?> c)
Elimina de este conjunto todos sus elementos que estén contenidos en la colección especificada (operación opcional). (Traducción automática)
final var src = Set.of("a", "b", "c");
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.<String>of())); // false
System.out.println(set); // [a, b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("a"))); // true
System.out.println(set); // [b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("a", "b"))); // true
System.out.println(set); // [c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("b", "a"))); // true
System.out.println(set); // [c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("a", "b", "c"))); // true
System.out.println(set); // []
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("X", "Y"))); // false
System.out.println(set); // [a, b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("a", "X"))); // true
System.out.println(set); // [b, c]
}
boolean retainAll (Collection<?> c)
Conserva únicamente los elementos de este conjunto que están contenidos en la colección especificada (operación opcional). (Traducción automática)
final var src = Set.of("a", "b", "c");
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.<String>of())); // true
System.out.println(set); // []
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("a"))); // true
System.out.println(set); // [a]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("a", "b"))); // true
System.out.println(set); // [a, b]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("b", "a"))); // true
System.out.println(set); // [a, b]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("a", "b", "c"))); // false
System.out.println(set); // [a, b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("a", "b", "c", "X", "Y"))); // false
System.out.println(set); // [a, b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("X", "a", "b", "Y"))); // true
System.out.println(set); // [a, b]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("X", "Y"))); // true
System.out.println(set); // []
}
int size ()
Devuelve el número de elementos de este conjunto (su cardinalidad). (Traducción automática)
final var set1 = Set.of();
System.out.println(set1); // []
System.out.println(set1.size()); // 0
final var set2 = Set.of("a", "b", "c");
System.out.println(set2); // [a, b, c]
System.out.println(set2.size()); // 3
default Spliterator<E> spliterator ()
Crea un divisor sobre los elementos de este conjunto. (Traducción automática)
final var set = Set.of("aaa", "bbb", "ccc");
System.out.println(set); // [bbb, ccc, aaa]
final var spliterator = set.spliterator();
System.out.println("-- forEachRemaining --");
spliterator.forEachRemaining(System.out::println);
// Result
// ↓
//-- forEachRemaining --
//bbb
//ccc
//aaa
Object[] toArray ()
Devuelve una matriz que contiene todos los elementos de este conjunto. (Traducción automática)
final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]
final Object[] array = set.toArray();
System.out.println(Arrays.toString(array)); // [a, b, c]
<T> T[] toArray (T[] a)
Devuelve una matriz que contiene todos los elementos de este conjunto; el tipo de tiempo de ejecución de la matriz devuelta es el de la matriz especificada. (Traducción automática)
final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]
final String[] ret = set.toArray(new String[0]);
System.out.println(Arrays.toString(ret)); // [a, b, c]
final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]
{
final String[] array = new String[3];
System.out.println(Arrays.toString(array)); // [null, null, null]
final var ret = set.toArray(array);
System.out.println(Arrays.toString(array)); // [a, b, c]
System.out.println(Arrays.toString(ret)); // [a, b, c]
}
{
final String[] array = new String[5];
System.out.println(Arrays.toString(array)); // [null, null, null, null, null]
final var ret = set.toArray(array);
System.out.println(Arrays.toString(array)); // [a, b, c, null, null]
System.out.println(Arrays.toString(ret)); // [a, b, c, null, null]
}
{
final String[] array = new String[1];
System.out.println(Arrays.toString(array)); // [null]
final var ret = set.toArray(array);
System.out.println(Arrays.toString(array)); // [null]
System.out.println(Arrays.toString(ret)); // [a, b, c]
}
Methods declared in Collection
Methods declared in Iterable
Related posts
- Ejemplos de API