English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

¿Cómo convertir una colección de enteros en un array de int en Java?


Los objetos de colección en Java son objetos que almacenan referencias a otros objetos. El paquete java.util proporciona clases e interfaces de colección. Hay cuatro interfaces de colección principales, es decir, lista, cola, mapeo.

Set - Un objeto Set es una colección que almacena un grupo de elementos, que crece dinámicamente y no permite elementos repetidos.

HashSet y LinkedHashSet son clases que implementan la interfaz Set. Puede crear un objeto Set implementando una de estas clases.

Ejemplo

import java.util.HashSet;
public class SetExample {
   public static void main(String args[]) {
      //Instanciar HashSet
      HashSet<String> hashSet = new HashSet<String>();
      //Rellenar HashSet
      hashSet.add("Mango");
      hashSet.add("Apple");
      hashSet.add("Cherries");
      hashSet.add("Banana");
      System.out.println(hashSet);
   }
}

Resultado de salida

[Apple, Mango, Cherries, Banana]

Convertir un objeto Set en un array

Se pueden usar varias formas para convertir un objeto de conjunto en un array-

Agregar cada elemento-Se puede usar un bucle foreach para agregar cada elemento del objeto Set al array.

Ejemplo

import java.util.HashSet;
import java.util.Set;
public class SetExample {
   public static void main(String args[]) {
      //Instanciar HashSet
      Set<Integer> hashSet = new HashSet<Integer>();
      //Rellenar HashSet
      hashSet.add(1124);
      hashSet.add(3654);
      hashSet.add(7854);
      hashSet.add(9945);
      System.out.println(hashSet);
      //Crear un array de enteros vacío
      Integer[] array = new Integer[hashSet.size()];
      //Convertir un objeto de conjunto en un array de enteros
      int j = 0;
      for (Integer i: hashSet) {
         array[j++] = i;
      }
   }
}

Resultado de salida

[1124, 3654, 9945, 7854]

Usar el método toArray() -El método toArray() de la interfaz Set acepta un array, llena ese array con todos los elementos del conjunto actual y luego lo devuelve. Al usar este método, se puede convertir un objeto Set en un array.

Ejemplo

import java.util.HashSet;
import java.util.Set;
public class SetExample {
   public static void main(String args[]) {
      //Instanciar HashSet
      Set<Integer> hashSet = new HashSet<Integer>();
      //Rellenar HashSet
      hashSet.add(1124);
      hashSet.add(3654);
      hashSet.add(7854);
      hashSet.add(9945);
      //Crear un array de enteros vacío
      Integer[] array = new Integer[hashSet.size()];
      //Convertir un objeto Set en un array de enteros
      hashSet.toArray(array);
      System.out.println(Arrays.toString(array));
   }
}

Resultado de salida

[1124, 3654, 9945, 7854]

Usar Java8Porque se introdujo Java8Flujos, y estos flujos proporcionan métodos para convertir objetos de conjunto en arrays.

Ejemplo

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class SetExample {
   public static void main(String args[]) {
      //Instanciar HashSet
      Set<Integer> hashSet = new HashSet<Integer>();
      //Rellenar HashSet
      hashSet.add(1124);
      hashSet.add(3654);
      hashSet.add(7854);
      hashSet.add(9945);
      System.out.println(hashSet);
      //Crear un array de enteros vacío
      Integer[] array = hashSet.stream().toArray(Integer[]::new);
      System.out.println(Arrays.toString(array));
   }
}

Resultado de salida

[1124, 3654, 9945, 7854]