Java TreeSet lower(E e) Example

The last() method of java.util.TreeSet class will return the last (highest) element currently in this set whereas lower(E e) method returns the greatest element in this set strictly less than the given element, or null if there is no such element.

Signature

public E lower(E e) 

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

Example

The following example shows the usage of java.util.TreeSet.lower(e) method.

import java.util.TreeSet;

public class LowerMethodTreeSetExample 
{
    public static void main(String args[])
    {
        // Create treeSet1 object
        TreeSet treeSet1 = new TreeSet();
        
        //adding elements to TreeSet using add()
        treeSet1.add("Element1");
        treeSet1.add("Element5");
        treeSet1.add("Element4");
        treeSet1.add("Element2");
        
        //Print Element lower than "Element2"
        System.out.println("Element Lower than \"Element2\" is : "+treeSet1.lower("Element2"));
    }
}

Output

Element Lower than "Element2" is : Element1

Leave a Reply

Your email address will not be published. Required fields are marked *