Skip to content
Related Articles

Related Articles

Improve Article
TreeSet lower() method in Java
  • Difficulty Level : Medium
  • Last Updated : 23 Dec, 2018

The lower(E ele) method of TreeSet class in Java is used to return the greatest element in this set which is strictly less than the given element. If no such element exists in this TreeSet collection then this method returns a NULL.

Here, E is the type of the elements maintained by this collection.

Syntax:

public E lower(E ele)

Parameters: It takes only one parameter ele. It is the element based on which the greatest value in the set which is strictly less than this value is determined.

Return Value: It returns a value of type E which is either null or the required value.



Exceptions:

  • ClassCastException: This method throws a ClassCastException if the specified element cannot be compared with the elements of the set.
  • NullPointerException: This method throws a NullPointerException if the given element is null and the set uses natural ordering or the comparator does not permit null values.

Below programs illustrate the lower() method :

Program 1:




// Java program to illustrate lower() method
// of TreeSet class
  
import java.util.TreeSet;
public class GFG {
    public static void main(String args[])
    {
        TreeSet<Integer> tree = new TreeSet<Integer>();
  
        // Add elements to this TreeSet
        tree.add(10);
        tree.add(5);
        tree.add(8);
        tree.add(1);
        tree.add(11);
        tree.add(3);
  
        System.out.println(tree.lower(15));
    }
}

Output:

11

Program 2 (demonstration of NullPointerException):




// Java program to illustrate lower() method
// of TreeSet class
  
import java.util.TreeSet;
public class GFG {
    public static void main(String args[])
    {
        TreeSet<String> tree = new TreeSet<String>();
  
        try {
  
            // Add elements to TreeSet
            tree.add("10");
            tree.add("5");
            tree.add("8");
            tree.add("1");
            tree.add("11");
            tree.add("3");
  
            System.out.println(tree.lower(null));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output:

java.lang.NullPointerException
    at java.util.TreeMap.compare(TreeMap.java:1294)
    at java.util.TreeMap.getLowerEntry(TreeMap.java:494)
    at java.util.TreeMap.lowerKey(TreeMap.java:711)
    at java.util.TreeSet.lower(TreeSet.java:414)
    at GFG.main(GFG.java:20)

Program 3 (demonstration of ClassCastException):




// Java program to illustrate lower() method
// of TreeSet class
  
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;
  
public class GFG {
    public static void main(String args[])
    {
        TreeSet<List> tree = new TreeSet<List>();
  
        List<Integer> l1 = new LinkedList<Integer>();
        l1.add(1);
        l1.add(2);
        tree.add(l1);
  
        List<Integer> l2 = new LinkedList<Integer>();
        l2.add(3);
        l2.add(4);
  
        List<Integer> l3 = new ArrayList<Integer>();
        l2.add(5);
        l2.add(6);
  
        try {
            System.out.println(tree.lower(l3));
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output:

Exception in thread "main" java.lang.ClassCastException: 
java.util.LinkedList cannot be cast to java.lang.Comparable
    at java.util.TreeMap.compare(TreeMap.java:1294)
    at java.util.TreeMap.put(TreeMap.java:538)
    at java.util.TreeSet.add(TreeSet.java:255)
    at GFG.main(GFG.java:17)

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html#lower(E)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :