Junior — Middle
Have you worked with the TreeSet class for data storage?
sobes.tech AI
Answer from AI
Yes, the TreeSet class in Java is used to store unique elements in sorted order. It implements the SortedSet interface and is based on a red-black tree.
Main features:
- Elements are automatically sorted according to their natural order or by a provided comparator.
- It does not allow duplicates.
- Operations like add, remove, and search are performed in O(log n) time.
Example usage:
TreeSet<String> set = new TreeSet<>();
set.add("apple");
set.add("banana");
set.add("apple"); // will not be added, as it is a duplicate
for (String fruit : set) {
System.out.println(fruit);
}
// Output:
// apple
// banana