VOOZH about

URL: https://www.geeksforgeeks.org/java/java-treeset-special-methods/

⇱ Java TreeSet Special Methods - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java TreeSet Special Methods

Last Updated : 6 Feb, 2023

TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The TreeSet implements a NavigableSet interface by inheriting AbstractSet class. This means that the elements stored in a TreeSet are in an ordered way, i.e. in the ascending order. Because of this feature of TreeSet, it provides certain amazing methods of NavigableSet interface apart from those traditionally provided by the Collection interface.

Note: Since TreeSet is the implementation of Set, therefore it does not allow duplication of elements.

Some of specific methods provided by TreeSet are as follows:

  1. floor() Method
  2. lower() Method
  3. ceiling() Method
  4. higher() Method
  5. subSet() Method
  6. headSet() Method
  7. tailSet() Method

Now let us discuss all methods along with their implementation which is as follows

Method 1: floor() Method

This method returns the greatest element in the set less than or equal to the given element, or null if there is no such element.

Arguments: A number less than or equal value needs to be searched for

Syntax:

treeSetObject.floor(argument) ;

Example:


Output
Tree set = [12, 37, 54, 68, 98]
floor(60) = 54
floor(54) = 54

Method 2: lower() Method

This method returns the greatest element in this set strictly less than the given element, or null if there is no such element.

Argument: Number whose less than value needs to be found.

Syntax: 

treeSetObject.lower(argument) ;

Example:


Output
[12, 37, 54, 68, 98]
lower(90) = 68
lower(68) = 54

Method 3: ceiling() Method

This method returns the least element in this set greater than or equal to the given element, or null if there is no such element.

Argument: Number whose less than or equal value needs to be found.

Syntax: 

treeSetObject.ceiling(argument) ;

Example:


Output
tree set = [12, 37, 54, 68, 98]
ceiling(50) = 54
ceiling(68) = 68
ceiling(100) = null

Method 4: higher() Method  

This method returns the least element in this set strictly greater than the given element, or null if there is no such element.

Argument: The number whose less than value needs to be found.

Syntax:

treeSetObject.higher(argument) ;

Example:


Output
tree set = [12, 37, 54, 68, 98]
higher(50) = 54
higher(68) = 98
higher(98) = null

Method 5: subSet() Method

This method will return elements ranging from 'fromElement' to 'toElement'.

Note: 'fromElement' is inclusive and 'toElement' is exclusive.

Syntax:

treeSetObject.subSet(fromElement, toElement);
// where fromElement is the lower limit (inclusive) and 
// toElement is the upper limit (exclusive) 
// of the set containing values between these limits

Example:


Output
tree set = [12, 37, 54, 68, 98]
Elements between 54 and 98 are : [54, 68]
Elements between 54 and 98 including both the limits : [54, 68, 98]
Elements between 54 (exclusive) and 98 (inclusive) : [68, 98]
Elements between 54 (exclusive) and 98 (exclusive) : [68]

Method 6: headSet() Method

This method will return elements of TreeSet which are less than the specified element.

Syntax:

treeSetObject.headSet(upToNumber) ;

Note: 'upToNumber' is the upper limit of the numbers to be found (exclusive)

Example:


Output
tree set = [12, 37, 54, 68, 98]
Elements upto 90 : [12, 37, 54, 68]
Elements upto 68 : [12, 37, 54]
Elements upto 68 (inclusive) : [12, 37, 54, 68]

Method 7: 

This method will return elements of TreeSet which are greater than or equal to the specified element. 

Syntax:

treeSetObject.tailSet(aboveThisNumber) ;

Note: 'aboveThisNumber' is the lower limit of the numbers to be found (inclusive).

Example:


Output
tree set = [12, 37, 54, 68, 98]
Elements above 50 : [54, 68, 98]
Elements above 54 : [54, 68, 98]
Elements above 54 (exclusive) : [68, 98]
Comment
Article Tags: