Program:
import java.util.*; public class SetExample { public static void main(String[] args) { SortedSet<String> sortedSet = new TreeSet<>() ; // Creating sortedSet object // Adding elements to sorted set object sortedSet.add("Example"); sortedSet.add("Code"); sortedSet.add("A"); // Displaying the output System.out.println(sortedSet); // Removing A element from the set sortedSet.remove("A"); System.out.println(sortedSet); // printing first element from the set System.out.println(sortedSet.first()); // printing last element from the set System.out.println(sortedSet.last()); } }
Output:
[A, Code, Example] [Code, Example] Code Example
This post was last modified on July 11, 2023