dsa

Java SortedSet Interface

In this tutorial, we will learn about the SortedSet interface in Java and its methods with the help of an example.

The SortedSet interface of the Java Collections framework is used to store elements with some order in a set. It extends the Set interface.

The SortedSet interface present in java.util package extends the Set interface present in the collection framework. It is an interface that implements the mathematical set. This interface contains the methods inherited from the Set interface and adds a feature that stores all the elements in this interface to be stored in a sorted manner.

sortedSet

Example:

// Java program to demonstrate the 
// Sorted Set 
import java.util.*; 

class SortedSetExample{ 

	public static void main(String[] args) 
	{ 
		SortedSet<String> ts 
			= new TreeSet<String>(); 

		// Adding elements into the TreeSet 
		// using add() 
		ts.add("India"); 
		ts.add("Australia"); 
		ts.add("South Africa"); 

		// Adding the duplicate 
		// element 
		ts.add("India"); 

		// Displaying the TreeSet 
		System.out.println(ts); 

		// Removing items from TreeSet 
		// using remove() 
		ts.remove("Australia"); 
		System.out.println("Set after removing "
						+ "Australia:" + ts); 

		// Iterating over Tree set items 
		System.out.println("Iterating over set:"); 
		Iterator<String> i = ts.iterator(); 
		while (i.hasNext()) 
			System.out.println(i.next()); 
	} 
} 

output:
[Australia, India, South Africa]
Set after removing Australia:[India, South Africa]
Iterating over set:
India
South Africa

Methods in Java LinkedHashMap

Method Description
*add(element) This method is used to add a specific element to the set. The function adds the element only if the specified element is not already present in the set else the function returns False if the element is already present in the Set.
*addAll(collection) This method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order.
*clear() This method is used to remove all the elements from the set but not delete the set. The reference for the set still exists.
comparator() This method returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.
*contains(element) This method is used to check whether a specific element is present in the Set or not.
*containsAll(collection) This method is used to check whether the set contains all the elements present in the given collection or not. This method returns true if the set contains all the elements and returns false if any of the elements are missing.
first() This method returns the first(lowest) element present in this set.
hashCode() This method is used to get the hashCode value for this instance of the Set. It returns an integer value which is the hashCode value for this instance of the Set.
headSet(element) This method returns the elements which are less than the element that are present in the sorted set.
*isEmpty() This method is used to check if a SortedSet is empty or not.
last() This method returns the last(highest) element present in the set.
*remove(element) This method is used to remove the given element from the set. This method returns True if the specified element is present in the Set otherwise it returns False.
*removeAll(collection) This method is used to remove all the elements from the collection which are present in the set. This method returns true if this set changed as a result of the call.
*retainAll(collection) This method is used to retain all the elements from the set which are mentioned in the given collection. This method returns true if this set changed as a result of the call.
*size() This method is used to get the size of the set. This returns an integer value which signifies the number of elements.
subSet(element1, element2) This method returns a sorted subset from the set containing the elements between element1 and element2.
tailSet(element) This method returns the elements which are greater than or equal to the element that are present in the sorted set.
*toArray() This method is used to form an array of the same elements as that of the Set.