import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
public class TriHashSet {
public static void main(String[] args) {
//create a hashset
HashSet< String> hset = neues HashSet< String> ();
//Elemente hinzufügen
hset.add("a2");
hset.add("a3");
hset.add("b1");
hset.add("b3");
hset.add("a1");
hset.add("b2");
System.out.println("Vor dem Sortieren:");
for(String s: hset)
System.out.println(s);
//Kopieren von HashSet-Elementen in ArrayList
List< String> list = new ArrayList< String> (hset);
Sammlungen.sort(Liste);
System.out.println("Nach dem Sortieren:");
for(String s: hset)
System.out.println(s);
}
}
Vor der Sortierung:
b1
a1
b3
b2
a2
a3
Nach der Sortierung:
b1
a1
b3
b2
a2
a3
import java.util.HashSet als Argument;Ausgabe:
import java.util.TreeSet;
public class TriHashSetTreeMap {
public static void main(String[] args) {
//create a hashset
HashSethset = new HashSet ();
//Elemente hinzufügen
hset.add(5);
hset.add(16);
hset.add(8);
hset.add(22);
hset.add(14);
hset.add(11);
System.out.println("Vor dem Sortieren: "+hset);
for(int n: hset)
System.out.println(n);
//Kopieren von HashSet-Elementen in ArrayList
TreeSettreeset = new TreeSet< > (hset);
System.out.println("Nach dem Sortieren: "+treeset);
for(int n : treeset)
System.out.println(n);
}
}
Vor der Sortierung: [16, 5, 22, 8, 11, 14]
16
5
22
8
11
14
Nachher IR: [5, 8, 11, 14, 16, 22]
5
8
11
14
16
22
Please disable your ad blocker and refresh the window to use this website.