Java TreeSet : ceiling(), floor(), headSet(), tailSet()

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

The floor() method returns the greatest element in this set less than or equal to the given element, or null if there is no such element.

The headset() method returns a view of the portion of this set whose elements are strictly less than toElement.

The tailSet() method returns a view of the portion of this set whose elements are greater than or equal to fromElement.

Program

0

 

 

 

Progarm Source

import java.util.TreeSet;

public class Javaapp {

    public static void main(String[] args) {
        
        TreeSet ts = new TreeSet();
        ts.add(20);
        ts.add(40);
        ts.add(60);
        ts.add(80);
        System.out.println("ceiling(15)  : "+ts.ceiling(15));
        System.out.println("ceiling(25)  : "+ts.ceiling(25));
        System.out.println("floor(65)    : "+ts.floor(65));
        System.out.println("floor(85)    : "+ts.floor(85));
        System.out.println("headSet(65)  : "+ts.headSet(65));
        System.out.println("tailSet(25)  : "+ts.tailSet(25));
    }
}

Leave a Comment