Sorted Set Interface Example Java Program
Definition
Unlike a regular set, the elements in a sorted set are sorted, either by the element's compareTo() method, or a method provided to the constructor of the sorted set. The first and last elements of the sorted set can be retrieved, and subsets can be created via minimum and maximum values, as well as the beginning or ending at the beginning or end of the sorted set. The SortedSet interface is implemented by TreeSet.
Syntax
SortedSet<variable-type> variable-name= new TreeSet<variable-type>();
Sorted Set Interface Example Program
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;
public class SortedSetInterfaceExample {
public static void main(String[] args) {
SortedSet<String> sortedSet = new TreeSet<String>();
System.out.println("Enter the input to be added in Sorted Set : ");
Scanner scanner = new Scanner(System.in);
String val1 = scanner.nextLine();
String val2 = scanner.nextLine();
String val3 = scanner.nextLine();
String val4 = scanner.nextLine();
sortedSet.add(val1);
sortedSet.add(val2);
sortedSet.add(val3);
sortedSet.add(val4);
System.out.println(sortedSet);
}
}
Sample Output
Enter the input to be added in Sorted Set :
zoooooo
boooooo
noooooo
yoooooo
[boooooo, noooooo, yoooooo, zoooooo]
Java Collection Programs
- Linked List Example Java Program
- ArrayList Example Java Program
- HashSet Example Java Program
- Clear an Arraylist Example Java Program
- Reversing an ArrayList Example Java Program
- Linked HashSet Example Java Program
- HashMap Example Java Program
- Set Value in ArrayList Example Java Program
- Linked HashMap Example Java Program
- Remove Element from ArrayList Example Java Program
- Identity HashMap Example Java Program
- TreeMap Example Java Program
- TreeSet Example Java Program
- Finding Duplicates in Array Using TreeSet Java Example Program
- HashTable Example Java Program
- EnumSet Example Java Program
- Enum Map Example Java Program
- Wrapper Example Java Program
- Un Modifiable Wrapper Example Java Program
- Iterator Interface Example Java Program
- Sorted Map Interface Example Java Program
- Sorted Set Interface Example Java Program
Read More Articles
- Multiple Inheritance Using Interface Example Java Program
- Single Inheritance Example Java Program
- Multilevel Inheritance Example Java Program
- Hierarchical Inheritance Example Java Program
- Find all Substrings of a given string Example Java Program
- Sum Of Three Numbers Example Java Program
- Create Matrix Example Java Program
- Twin Prime Example Java Program
- Compile Time Polymorphism Example Java Program
- Encapsulation Example Java Program