The SortedSet interface is part of the java.util package and extends the Set interface from the collection framework. The SortedSet interface in the Java Collections framework is used to store elements in a set according to some order.
Post On: | SortedSet In Java |
Post Type: | Java Tutorials |
Published On: | www.softwaretestingo.com |
Applicable For: | Freshers & Experience |
Get Updates: | SoftwareTestingo Telegram Group |
A set is used to provide a particular ordering on its element. The elements are ordered either by using a natural ordering or by using a Comparator. All the elements which are inserted into a sorted set must implement the Comparable interface in order for them to be properly sorted.
The set’s iterator will go through the set in ascending order. There are several other operations that can be used to make the most of the ordering. All elements must be able to be compared with each other.
The class that implements SortedSet
In order to use the functionalities of the SortedSet interface, we need to use the TreeSet class that implements it.
How to use SortedSet?
In order to use SortedSet, we must first import the java.util.SortedSet package.
// SortedSet implementation by TreeSet class SortedSet<String> testing= new TreeSet<>();
Performing Various Operations on SortedSet
SortedSet is an interface, which means it can only be used with a class that implements the interface. TreeSet is a class that implements SortedSet. Here’s how to do some common operations on TreeSet.
- Adding Elements
- Accessing the Elements
- Removing the Values
- Iterating through the SortedSet
Adding Elements
To add an element to the SortedSet, we can use the add() method. However, please keep in mind that the insertion order is not retained in the TreeSet. This is because internally, for every element, the values are compared and sorted in ascending order.
It’s important to keep in mind that duplicate elements are not allowed and will be ignored. Additionally, SortedSet does not accept null values.
package com.SoftwareTestingO.collections; import java.util.SortedSet; import java.util.TreeSet; public class AddingElementInSortedSet { public static void main(String[] args) { SortedSet<String> obj= new TreeSet<String>(); // Elements are added using add() method obj.add("Learn"); obj.add("Software"); obj.add("Testing"); obj.add("Learn"); // Printing All the Elements of SortedSet System.out.println(obj); } }
Accessing the Elements:
After adding elements to a data structure, we can use inbuilt methods like contains(), first(), last(), etc. to access the elements.
package com.SoftwareTestingO.collections; import java.util.SortedSet; import java.util.TreeSet; public class AccessingElementInSortedSet { public static void main(String[] args) { SortedSet<String> obj= new TreeSet<String>(); // Elements are added using add() method obj.add("Learn"); obj.add("Software"); obj.add("Testing"); obj.add("Learn"); // Printing All the Elements of SortedSet System.out.println(obj); String check = "Blog"; // Check if the above string exists in the SortedSet System.out.println("Contains: " + check + " - " + obj.contains(check)); // Print the first element in the SortedSet System.out.println("First Value is: " + obj.first()); // Print the last element in the SortedSet System.out.println("Last Value is: " + obj.last()); } }
Removing the Values:
You can remove values from the SortedSet by using the remove() method.
package com.SoftwareTestingO.collections; import java.util.SortedSet; import java.util.TreeSet; public class RemoveElementInSortedSet { public static void main(String[] args) { SortedSet<String> obj= new TreeSet<String>(); // Elements are added using add() method obj.add("Learn"); obj.add("Software"); obj.add("Testing"); obj.add("www.SoftwareTestingo.com"); System.out.println("Before Removing Element: " + obj); // Removing the element Testing obj.remove("Testing"); System.out.println("Before Removing Element: " + obj); } }
Iterating through the SortedSet:
There are various ways to iterate through the SortedSet. The most common way is to use the enhanced for loop.
package com.SoftwareTestingO.collections; import java.util.SortedSet; import java.util.TreeSet; public class IteratingElementsinSortedSet { public static void main(String[] args) { SortedSet<String> obj= new TreeSet<String>(); // Elements are added using add() method obj.add("Unit Testing"); obj.add("UAT Testing"); obj.add("Testing"); obj.add("Automation"); // Iterating though the SortedSet for (String value : obj) System.out.print(value+ ", "); System.out.println(); } }
Methods of SortedSet
The SortedSet interface includes all the methods of the Set interface. This is because Set is a super interface of SortedSet. In addition to methods included in the Set interface, the SortedSet interface also includes these methods:
Method | Description |
---|---|
Comparator comparator( ) | Returns the invoking sorted set’s comparator. If the natural ordering is used for this set, null is returned. |
Object first( ) | Returns the first element in the invoking sorted set. |
SortedSet headSet(Object end) | Returns a SortedSet containing those elements less than the end that are contained in the invoking sorted set. Elements in the returned sorted set are also referenced by the invoking sorted set. |
Object last( ) | Returns the last element in the invoking sorted set. |
SortedSet subSet(Object start, Object end) | Returns a SortedSet that includes those elements between the start and end.1. Elements in the returned collection are also referenced by the invoking object. |
SortedSet tailSet(Object start) | Returns a SortedSet that contains those elements greater than or equal to the start that is contained in the sorted set. Elements in the returned set are also referenced by the invoking object. |
package com.SoftwareTestingO.collections; import java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet; public class SortedSetExample { public static void main(String[] args) { SortedSet<String> obj= new TreeSet<String>(); // using add() method add elements into the TreeSet obj.add("Testing"); obj.add("Manual Testing"); obj.add("Automation Testing"); // Displaying the TreeSet Before Adding the Duplicate element System.out.println("Before Adding the Duplicate element" +obj); // Adding the Duplicate Element obj.add("Testing"); // Displaying the TreeSet After Adding the Duplicate element System.out.println("After Adding the Duplicate element"+obj); // Removing items from TreeSet obj.remove("Manual Testing"); System.out.println("Set after removing "+ "Manual Testing:" + obj); // Iterating over Tree set items System.out.print("Iterating over set:"); Iterator<String> i = obj.iterator(); while (i.hasNext()) System.out.print(i.next()+" "); } }
Leave a Reply