TreeSet : first(), last(), lower(), higher(), descendingSet()

The first() method returns the first (lowest) element currently in this set.

The last() method returns the last (highest) element currently in this set.

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

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

The descendingSet() method returns a reverse order view of the elements contained in this set.

Program

0

 

 

 

Program 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("ts              : "+ts);
        System.out.println("first()         : "+ts.first());
        System.out.println("last()          : "+ts.last());
        System.out.println("lower(80)       : "+ts.lower(80));
        System.out.println("higher(20)      : "+ts.higher(20));
        System.out.println("descendingSet() : "+ts.descendingSet());
    }
}

Leave a Comment