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

Come convertire un insieme di interi in un array di int in Java?


Gli oggetti di raccolta in Java sono oggetti che contengono riferimenti ad altri oggetti. Il pacchetto java.util fornisce classi e interfacce per le raccolte. Ci sono quattro principali interfacce di raccolta, ovvero collection, list, queue e map.

Set - L'oggetto set è una raccolta di elementi che cresce dinamicamente e non permette elementi duplicati.

HashSet e LinkedHashSet sono classi che implementano l'interfaccia Set. Puoi creare un oggetto Set implementando una di queste classi.

Esempio

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

Risultato di output

[Apple, Mango, Cherries, Banana]

Converti l'oggetto Set in un array

Puoi convertire l'oggetto di集合 in un array in molti modi -

Aggiungi ogni elemento - Puoi aggiungere ogni elemento dell'oggetto Set all'array utilizzando un ciclo foreach.

Esempio

import java.util.HashSet;
import java.util.Set;
public class SetExample {
   public static void main(String args[]) {
      //Istanzia HashSet
      Set<Integer> hashSet = new HashSet<Integer>();
      //Riempi HashSet
      hashSet.add(1124);
      hashSet.add(3654);
      hashSet.add(7854);
      hashSet.add(9945);
      System.out.println(hashSet);
      //Creare un array di interi vuoto
      Integer[] array = new Integer[hashSet.size()];
      //Converti l'oggetto di集合 in un array di interi
      int j = 0;
      for (Integer i: hashSet) {
         array[j++] = i;
      }
   }
}

Risultato di output

[1124, 3654, 9945, 7854]

Usare il metodo toArray() -Il metodo toArray() dell'interfaccia Set accetta un array e riempie questo array con tutti gli elementi dell'oggetto set attuale, quindi lo restituisce. Utilizzando questo metodo, è possibile convertire un oggetto Set in un array.

Esempio

import java.util.HashSet;
import java.util.Set;
public class SetExample {
   public static void main(String args[]) {
      //Istanzia HashSet
      Set<Integer> hashSet = new HashSet<Integer>();
      //Riempi HashSet
      hashSet.add(1124);
      hashSet.add(3654);
      hashSet.add(7854);
      hashSet.add(9945);
      //Creare un array di interi vuoto
      Integer[] array = new Integer[hashSet.size()];
      //Converti l'oggetto Set in un array di interi
      hashSet.toArray(array);
      System.out.println(Arrays.toString(array));
   }
}

Risultato di output

[1124, 3654, 9945, 7854]

Usare Java8: poiché è stato introdotto Java8 stream, e questi stream offrono metodi per convertire oggetti di集合 in array.

Esempio

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class SetExample {
   public static void main(String args[]) {
      //Istanzia HashSet
      Set<Integer> hashSet = new HashSet<Integer>();
      //Riempi HashSet
      hashSet.add(1124);
      hashSet.add(3654);
      hashSet.add(7854);
      hashSet.add(9945);
      System.out.println(hashSet);
      //Creare un array di interi vuoto
      Integer[] array = hashSet.stream().toArray(Integer[]::new);
      System.out.println(Arrays.toString(array));
   }
}

Risultato di output

[1124, 3654, 9945, 7854]