TreeSet lower() method in Java Last Updated : 23 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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 // 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 // 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 // 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) Comment More infoAdvertise with us Next Article TreeSet lower() method in Java P psil123 Follow Improve Article Tags : Java Java - util package java-treeset java-treeset-functions Practice Tags : Java Similar Reads TreeSet in Java TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree(red - black tree) for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equ 13 min read TreeSet add() Method in Java The Java.util.TreeSet.add() method in Java TreeSet is used to add a specific element into a TreeSet. The function adds the element only if the specified element is not already present in the set else the function return False if the element is not present in the TreeSet. Syntax: Tree_Set.add(Object 1 min read TreeSet addAll() Method in Java The java.util.TreeSet.addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any typ 2 min read TreeSet ceiling() method in Java with Examples The ceiling() method of java.util.TreeSet<E> class is used to return the least element in this set greater than or equal to the given element, or null if there is no such element. Syntax: public E ceiling(E e) Parameters: This method takes the value e as a parameter which is to be matched. Ret 2 min read TreeSet clear() Method in Java The Java.util.TreeSet.clear() method is used to remove all the elements from a TreeSet. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing TreeSet. Syntax: Tree_Set.clear() Pa 1 min read TreeSet clone() Method in Java The Java.util.TreeSet.clone() method is used to return a shallow copy of the mentioned tree set. It just creates a copy of the set. Syntax: Tree_Set.clone() Parameters: The method does not take any parameters. Return Value: The function just returns a copy of the TreeSet. Below program illustrates t 1 min read TreeSet contains() Method in Java With Examples TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equals if it is to co 3 min read TreeSet descendingIterator() method in Java with Examples The descendingIterator() method of java.util.TreeSet<E> class is used to return an iterator over the elements in this set in descending order.Syntax: public Iterator descendingIterator()Return Value: This method returns an iterator over the elements in this set in descending order.Below are th 2 min read TreeSet descendingSet() method in Java with Examples The descendingSet() method of java.util.TreeSet<E> class is used to return a reverse order view of the elements contained in this set. The descending set is backed by this set, so changes to the set are reflected in the descending set, and vice-versa. If either set is modified while an iterati 2 min read TreeSet first() Method in Java The Java.util.TreeSet.first() method is used to return the first of the element of a TreeSet. The first element here is being referred to the lowest of the elements in the set. If the elements are of integer types then the smallest integer is returned. If the elements are of the string types then th 3 min read Like