import java.util.SortedSet; import java.util.TreeSet; public class SortedSetDemo { public static void main(String[] args) { SortedSetnames = new TreeSet<>(); names.add("Alice"); names.add("Bob"); names.add("Charlie"); names.add("Dave"); String[] nameArray = names.toArray(new String[0]); for (String name : nameArray) { System.out.println(name); } } }
import java.util.SortedSet; import java.util.TreeSet; public class SortedSetDemo { public static void main(String[] args) { SortedSetThis code creates a TreeSet of integers, adds several integers to it, and then uses the toArray method to convert the set to an array. The resulting array is printed to the console. The SortedSet interface and its methods are part of the java.util package.numbers = new TreeSet<>(); numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); Integer[] numberArray = numbers.toArray(new Integer[0]); for (int i = 0; i < numberArray.length; i++) { System.out.println(numberArray[i]); } } }