import java.util.TreeSet; public class Main { public static void main(String[] args) { TreeSetnames = new TreeSet<>(); names.add("Alice"); names.add("Bob"); names.add("Cathy"); System.out.println("Before Clear: " + names); names.clear(); System.out.println("After Clear: " + names); } }
import java.util.concurrent.ConcurrentSkipListSet; public class Main { public static void main(String[] args) { ConcurrentSkipListSetOutput: Before Clear: [Alice, Bob, Cathy] After Clear: [] In this example, we create a ConcurrentSkipListSet of Strings and add some elements to it. We then print out the ConcurrentSkipListSet before and after calling the clear() method, showing that all elements have been removed. Package Library: java.util.concurrentnames = new ConcurrentSkipListSet<>(); names.add("Alice"); names.add("Bob"); names.add("Cathy"); System.out.println("Before Clear: " + names); names.clear(); System.out.println("After Clear: " + names); } }