U ovom odjeljku ćemo raspravljati kako pretvoriti Set ( HashSet ili TreeSet ) na popis ( ArrayList ili LinkedList ).
Postoje sljedeći načini pretvaranja Seta u List u Javi:
- Izvorni pristup
- Korištenje konstruktora (ArrayList ili LinkedList)
- Korištenje metode ArrayList addAll().
- Korištenje Stream u Javi
Izvorni pristup
Logika za nativni pristup je vrlo jednostavna. U ovom pristupu jednostavno kreiramo skup (HashSet ili TreeSet) i iteriramo preko skupa i dodajemo sve elemente skupa na popis uz pomoć petlje.
SetToListExample1.java
import java.util.*; public class SetToListExample1 { public static void main(String args[]) { //creating a Set of type String Set set = new HashSet(); //adding elements to the Set set.add('Bordeaux'); set.add('Cannes'); set.add('Chamonix'); set.add('Chartres'); set.add('Clermont-Ferrand'); set.add('Limoges'); set.add('Marseille'); set.add('Nice'); set.add('Paris'); //determines the size of Set int n = set.size(); //creates an ArrayList of size n List list = new ArrayList(n); //loop iterates over Set for (String x : set) //adds elements to the list list.add(x); System.out.println('ArrayList is: '); //loop iterates over List for (String x : list) //prints the elements System.out.println(x); } }
Izlaz:
ArrayList is: Cannes Bordeaux Marseille Nice Clermont-Ferrand Chartres Limoges Chamonix Paris
Korištenje konstruktora (klasa ArrayList ili LinkedList)
Logika je ista kao gore. Jedina razlika je u tome što smo koristili konstruktor klase ArrayList i LinkedList i proslijedili elemente skupa konstruktoru.
List list = new ArrayList(set); List lList = new LinkedList(set);
SetToListExample2.java
import java.util.*; public class SetToListExample2 { public static void main(String args[]) { //creating a HashSet of type String Set set= new HashSet(); //adding elements to the Set set.add('Sharjah'); set.add('Dubai'); set.add('Abu Dhabi'); set.add('Kalba'); set.add('Dhaid'); set.add('Hatta'); //creates an ArrayList using constructor and passing elements to the constructor List list = new ArrayList(set); System.out.println('ArrayList is: '); //loop iterates over the list for (String x : list) //prints the ArrayList System.out.println(x); System.out.println(' LinkedList is: '); //creates LinkedList using constructor and passing elements to the constructor List lList = new LinkedList(set); //loop iterates over LinkedList for (String x : lList) //prints the LinkedList System.out.println(x); } }
Izlaz:
ArrayList is: Sharjah Dhaid Kalba Hatta Dubai Abu Dhabi LinkedList is: Sharjah Dhaid Kalba Hatta Dubai Abu Dhabi
Korištenje metode ArrayList.addAll().
Metoda ArrayList.addAll() dodaje sve elemente u navedenoj kolekciji na kraj ovog popisa, redoslijedom kojim ih vraća Iterator navedene kolekcije. Ona nadjačava metodu addAll() klase AbstractCollection.
SetToListExample3.java
import java.util.*; public class SetToListExample3 { public static void main(String args[]) { //converting HashSet to ArrayList //creating a HashSet of type String Set set = new HashSet(); //adding elements to the Set set.add('Texas'); set.add('California'); set.add('Illinois'); set.add('Dallas'); set.add('San Jose'); set.add('California'); set.add('Austin'); set.add('Columbus'); //creates a constructor of the ArrayList class List list = new ArrayList(); //adding the set elements to the list using the addAll() method list.addAll(set); System.out.println('ArrayList is: '); //loop iterates over the LinkedList for (String x : list) //prints the ArrayList System.out.println(x); //converting HashSet to LinkedList List linkList = new LinkedList(); //adding Set elements to the LinkedList using the addAll() method linkList.addAll(set); System.out.println(' LinkedList is: '); //loop iterates over the LinkedList for (String x : linkList) //prints the LinkedList System.out.println(x); } }
Izlaz:
ArrayList is: Texas Illinois Columbus California Austin Dallas San Jose LinkedList is: Texas Illinois Columbus California Austin Dallas San Jose
Korištenje Streama u Javi
Ako koristimo Stream za pretvaranje Seta u Listu, prvo ćemo pretvoriti Set u stream, a zatim pretvoriti stream u popis. Radi samo u Javi 8 ili novijim verzijama.
List list = set.stream().collect(Collectors.toList());
tok(): Metoda stream() vraća regularni tok objekta skupa ili popisa.
Stream.collect(): Metoda collect() klase Stream koristi se za prikupljanje elemenata bilo kojeg Streama u kolekciju.
Collectors.toList(): Metoda vraća sakupljač koji prikuplja ulazne elemente u novi popis.
Pogledajmo primjer.
SetToListExample4.java
import java.util.*; import java.util.stream.*; public class SetToListExample4 { public static void main(String args[]) { //Creating a hash set of strings Set set = new HashSet(); //adding elements to the Set set.add('London'); set.add('England'); set.add('Wales'); set.add('Scotland'); set.add('Bristol'); set.add('Cambridge'); //converts set to stream and then stream to list List list = set.stream().collect(Collectors.toList()); System.out.println('List is: '); //loop iterates over the list for (String x : list) //prints the list elements System.out.println(x); } }
Izlaz:
List is: Cambridge Bristol Wales London England Scotland