logo

Java Nastavi na seznam

V tem razdelku bomo razpravljali kako pretvoriti Set ( HashSet oz TreeSet ) na seznam ( ArrayList oz LinkedList ).

Java Nastavi na seznam

Obstajajo naslednji načini za pretvorbo Set v List v Javi:

  • Domači pristop
  • Uporaba konstruktorja (ArrayList ali LinkedList)
  • Uporaba metode ArrayList addAll().
  • Uporaba Tok v Javi

Domači pristop

Logika domačega pristopa je zelo preprosta. Pri tem pristopu preprosto ustvarimo Set (HashSet ali TreeSet) in ga preletimo ter dodamo vse elemente Seta na seznam s pomočjo zanke.

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); } } 

Izhod:

 ArrayList is: Cannes Bordeaux Marseille Nice Clermont-Ferrand Chartres Limoges Chamonix Paris 

Uporaba konstruktorja (razred ArrayList ali LinkedList)

Logika je enaka kot zgoraj. Edina razlika je v tem, da smo uporabili konstruktor razreda ArrayList in LinkedList in konstruktorju posredovali elemente nabora.

 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); } } 

Izhod:

 ArrayList is: Sharjah Dhaid Kalba Hatta Dubai Abu Dhabi LinkedList is: Sharjah Dhaid Kalba Hatta Dubai Abu Dhabi 

Uporaba metode ArrayList.addAll().

Metoda ArrayList.addAll() doda vse elemente v podani zbirki na konec tega seznama v vrstnem redu, kot jih vrne iterator navedene zbirke. Preglasi metodo addAll() razreda 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); } } 

Izhod:

 ArrayList is: Texas Illinois Columbus California Austin Dallas San Jose LinkedList is: Texas Illinois Columbus California Austin Dallas San Jose 

Uporaba Stream v Javi

Če uporabimo Stream za pretvorbo Set v List, bomo najprej pretvorili Set v tok in nato tok pretvorili v seznam. Deluje samo v Javi 8 ali novejših različicah.

 List list = set.stream().collect(Collectors.toList()); 

tok(): Metoda stream() vrne običajni objektni tok nabora ali seznama.

Stream.collect(): Metoda collect() razreda Stream se uporablja za kopičenje elementov katerega koli toka v zbirko.

Collectors.toList(): Metoda vrne zbiralnik, ki zbere vhodne elemente v nov seznam.

Poglejmo primer.

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); } } 

Izhod:

 List is: Cambridge Bristol Wales London England Scotland