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