- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sorted Set Interface in Java with Examples
The Sorted Set interface has the term ‘Sorted’ in its name which signifies that it contains all the elements in ascending order. It extends the properties of the Set interface.
In this article, we will discuss the sorted set interface and perform some operations on it. But, in order to understand the SortedSet properly we need to understand the hierarchy of collection interface.
Collection Interface
In java, collection is an object or we can say a container for simplicity that allows us to group several numbers of objects in a single unit. The collection interface is present at the root of all collection framework interfaces. We can perform various operations on collection like adding, removing, iterating, searching and retrieving objects. Note that they can’t work with primitive datatypes like int or double. However, java provides wrapper classes that enable the use of primitive datatypes as objects. We will use those objects to work with collection interface.
Let’s discuss the sub interfaces of collection interface −
List − It is the sub interface of Java Collection Interface. It is a linear structure that stores and accesses each element in a sequential manner.
Set − It is the sub interface of Java Collection Interface that doesn’t allow duplicate values. It is similar to a mathematical set.
Queue − It provides the features of queue data structure. Queue follows the First in First out (FIFO) approach.
SortedSet Interface
As we have discussed earlier that it stores elements in ascending order. Since it extends Set interface therefore, it also doesn’t allow duplicate values and it has access to all the methods provided by Set interface.
Collection interfaces are available in ‘java.util’ package. To import this package into your program use the following command:
import java.util.*;
Here, ‘*’ signifies that we are importing all the classes available in this package.
To use the features of SortedSet, we will use tree set class that implements SortedSet interface.
Syntax
SortedSet< element_Type > collection_name = new TreeSet<>();
Here, element_Type is the wrapper class not primitive datatypes.
Other than the methods of Set interface it also includes the following methods −
first() − It returns the object present at the first position.
last() − It returns the object present at the last position.
subSet() − It takes two arguments and prints all objects available between the given arguments.
headSet() − It takes an argument and returns the objects from start to the specified argument.
tailSet() − It takes an argument and returns the objects from specified argument to the end of set.
Example 1
In the following example, we will create a Tree Set named ‘treeSt’ of type string and using the inbuilt method ‘add()’ of collection interface, we will add a few elements to the Tree Set. It will print the elements in ascending order irrespective of the order of input elements.
import java.util.*; public class Srtset { public static void main(String args[]) { // Creating tree set SortedSet<String> treeSt = new TreeSet<>(); // Adding elements in tree set treeSt.add("Tutorix"); treeSt.add("Simply"); treeSt.add("Easy"); treeSt.add("Learning"); treeSt.add("Tutorials"); treeSt.add("Point"); System.out.println("Elements of the given set: " + treeSt); } }
Output
Elements of the given set: [Easy, Learning, Point, Simply, Tutorials, Tutorix]
Example 2
The following example illustrates the use of all the inbuilt methods of SortedSet Interface we have discussed earlier in this article.
import java.util.*; public class Srtset { public static void main(String args[]) { // Creating tree set SortedSet<String> treeSt = new TreeSet<>(); // Adding elements in tree set treeSt.add("Tutorix"); treeSt.add("Simply"); treeSt.add("Easy"); treeSt.add("Learning"); treeSt.add("Tutorials"); treeSt.add("Point"); System.out.println("Elements in the given set: " + treeSt); String frst = treeSt.first(); System.out.println("Accessing First element of the given set: " + frst); String end = treeSt.last(); System.out.println("Accessing Last element of the given set: " + end); System.out.println("Accessing subsets of the given set: " + treeSt.subSet("Simply", "Tutorix")); System.out.println("Accessing first two elements of set: " + treeSt.headSet("Point")); System.out.println("Accessing last three elements of set: " + treeSt.tailSet("Simply")); } }
Output
Elements in the given set: [Easy, Learning, Point, Simply, Tutorials, Tutorix] Accessing First element of the given set: Easy Accessing Last element of the given set: Tutorix Accessing subsets of the given set: [Simply, Tutorials] Accessing first two elements of set: [Easy, Learning] Accessing last three elements of set: [Simply, Tutorials, Tutorix]
Conclusion
Since objects are stored in sorted i.e. ascending order the access and retrieval time becomes faster in the case of SortedSet Interface. Because of this excellent feature, SortedSet is frequently used to store large amounts of information that needs to be searched quickly.