Java TreeSet Special Method


The TreeSet class in Java environment is a container interface set that is mainly use to store tree. There are two different ways in this class.

  • An AbstractSet Class - It is a collection interface, part of the Java Collection Framework.

  • NavigableSet interface - It is a navigable collection set in Java Collection.

Here in the Java environment, TreeSet inherits AbstractSet class and carry through the NavigableSet. Some important aspects of TreeSet implementation −

  • It takes unique data as input.

  • Don't save insertion order of the data.

  • Sorts elements in an ascending order.

  • Thread unsafe.

Let’s discuss more criteria on TreeSet Class.

What Is A TreeSet Class And How It Works?

  • The TreeSet class provides the algorithm of a tree data structure in Java collection. This is a part of the SortedSet interface in Java. The ordering maintained by a natural manner and provides explicit comparator.

  • This process use a Red-Black tree; which is a self-balancing binary search tree model.

  • By this special tree method Search, remove, add consume O(log(N)) time operations can be handled.

  • In the TreeSet Class the synchronization always done in a manual way. That means more than one thread access a set of tree and then modify it.

  • After synchronizing some elements it encapsulates the sets naturally.

  • The access and retrieve work of a Java TreeSet is comparatively fast. And it is a great choice to conduct a search operation to access a huge amount of data.

  • In this TreeSet, any duplicate element is not allowed.

  • TreeSet(): A new and empty TreeSet will be created with sorting based on the natural ordering.

  • TreeSet(Collection<? extends E> c): A new TreeSet will be created with elements mentioned in the collection c and they will be sorted based on the natural ordering.

  • TreeSet(Comparator<? super E> comparator): A new and empty TreeSet will be created with sorting based on the mentioned comparator.

  • TreeSet(SortedSet<E> s): A new TreeSet will be created with elements mentioned in the sortedset s with ordering the same as that of it.

Algorithm

  • Step 1 − Create a new TreeSet class.

  • Step 2 − Extend a AbstractSet.

  • Step 3 − Add NavigableSet into it.

  • Setp 4 − Perform sorting with the input data.

  • Step 5 − Collection a new data set.

  • Step 6 − Iterate the whole data set.

TreeSet Special Methods - Syntax

public class TreeSet<Element E> extends AbstractSet<Element E> implements NavigableSet<Element E>, Cloneable, Serializable

The treemap of a TreeSet class contains with two interfaces.

  • SortedSet

  • NavigableSet

Data elements stored in an ascending order and nevigated by process.

Approaches to Achieve TreeSet Special Medthod In Java

  • Approach 1 − TreeSet Special Method Using Floor Method

  • Approach 2 − The Lower Method To Find An Element

  • Approach 3 − Find The Least Element By Using ceiling () Method

  • Approach 4 − Find a Data From A Tree Using higher() Method

Floor Method

The floor() method; returns the largest element present in the floor with “treeSetObject.floor(argument) ;” syntax. This method finds an element which is less than or equals to the element as input. It returns null when the method can't find the element as per the input.

Example 1

import java.util.Set;
import java.util.TreeSet;
public class Floorbytutorialp {
   public static void main(String[] args){
      TreeSet<Integer> treeSet= new TreeSet<>(Set.of(07, 16, 10, 1997, 2001, 2022));
      System.out.println("Tree set = " + treeSet);
      System.out.println("floor(16) = "+ treeSet.floor(16));
      System.out.println("floor(07) = "+ treeSet.floor(07));
   }
}

Output

Tree set = [7, 10, 16, 1997, 2001, 2022]
floor(16) = 16
floor(07) = 7

Lower Method

Lower() method returns the largest element which is strictly less than the given element as per the input. If null then no element present in the list.

The syntax followed here is

treeSetObject.lower(argument)

Example 2

import java.util.*;  
public class TreeSetLowerMethd {  
   public static void main(String as[]){  
      TreeSet <Integer>obj = new TreeSet<Integer>();  
      obj.add(16);     
      obj.add(07);  
      obj.add(10);  
      obj.add(97);  
      obj.add(2001);  
      System.out.println("TreeSet: " +obj);  
      System.out.println("lowest value then 7 : " +obj.lower(7));  
   }  
}

Output

TreeSet: [7, 10, 16, 97, 2001]
lowest value then 7 : null

Find The Least Element By Using ceiling () Method

In the Java TreeSet, the ceiling() returns the least data as an output.

The syntax is "treeSetObject.ceiling(argument) ;". In this method, the output is larger or equal to the input element, If not then it returns null.

Example 3

import java.util.Set;
import java.util.TreeSet;
public class GFG {
   public static void main(String[] args){
      TreeSet<Integer> treeSet= new TreeSet<>(Set.of(07, 16, 10, 2001, 1997));
      System.out.println("tree set = " + treeSet);
      System.out.println("ceiling(60) = "+ treeSet.ceiling(60));
      System.out.println("ceiling(70) = "+ treeSet.ceiling(70));
      System.out.println("ceiling(10) = "+ treeSet.ceiling(10));
   }
}

Output

tree set = [7, 10, 16, 1997, 2001]
ceiling(60) = 1997
ceiling(70) = 1997
ceiling(10) = 10

Find a Data from a Tree using higher() Method

The higher() method in the TreeSet method returns a number whose value is less than the input. If the desired data is not found, it will return NULL.

The syntax followed here is "treeSetObject.higher(argument) ;".

Example 1

import java.io.*;
import java.util.Set;
import java.util.TreeSet;
public class treeclasstp {
   public static void main(String[] args){
      TreeSet<Integer> treeSet= new TreeSet<>(Set.of(10, 07, 16, 2001, 97));
      System.out.println("tree set = " + treeSet);
      System.out.println("higher(1000) = "+ treeSet.higher(1000));
      System.out.println("higher(80) = "+ treeSet.higher(80));
      System.out.println("higher(5) = "+ treeSet.higher(5));
   }
}

Output

tree set = [7, 10, 16, 97, 2001]
higher(1000) = 2001
higher(80) = 97
higher(5) = 7

Conclusion

There are many types of Tree Set Special Methods available in the Java environment. Here in this article we have discussed some of them and built some Java code according to their syntax.

Updated on: 10-Apr-2023

104 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements