TreeSet higher(K) in Java

higher(K): This method is available in java.util.TreeSet class of Java.

Syntax:

K java.util.TreeSet.higher(K e)

This method takes one argument. This method returns the least element in this set strictly greater than the given element, or null if there is no such element.

Parameters: One parameter is required for this method.

e: the value to match.

Returns: the least element greater than e, or null if there is no such element.

Throws:

1. ClassCastException - if the specified element cannot be compared with the elements currently in the set.

2. NullPointerException - if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements.

Approach 1: When no exception

Java

import java.util.TreeSet;

public class TreeSethigher {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        String e = "C++";

        System.out.println(treeSet.higher(e));

    }
}

Output:

Hello


Approach 2: NullPointerException 

Java

import java.util.TreeSet;

public class TreeSethigher {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        String e = null;

        System.out.println(treeSet.higher(e));

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Comparable.compareTo(Object)" because "k1" is null at java.base/java.util.TreeMap.compare(TreeMap.java:1563) at java.base/java.util.TreeMap.getHigherEntry(TreeMap.java:461) at java.base/java.util.TreeMap.higherKey(TreeMap.java:1046) at java.base/java.util.TreeSet.higher(TreeSet.java:445)


TreeSet class Methods in Java Part -I

java.util.TreeSet

 A NavigableSet implementation based on a TreeMap.The elements are ordered using their natural ordering, or by a Comparator provided at a set creation time, depending on which constructor is used.

Some methods of TreeSet class.


add(String): This method adds the specified element to this set if it is not already present.


addAll(Collection): This method adds all of the elements in the specified collection to this set.


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


clear(): This method removes all of the elements from this set.


clone(): This method returns a shallow copy of this TreeSet instance.


comparator(): This method returns the comparator used to order the elements in this set,or null if this set uses the natural ordering of its elements.


contains(Object): This method returns true if this set contains the specified element.


descendingIterator(): This method returns an iterator over the elements in this set in descending order.


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


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


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


forEach(Consumer): This method performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.


headSet(K): This method returns a view of the portion of this set whose elements are strictly less than toElement.


headSet(K, boolean): This method returns a view of the portion of this set whose elements are less than(or equal to, if inclusive is true) toElement.


Some more methods of TreeSet class


TreeSet headSet(K, boolean) in Java

headSet(K, boolean): This method is available in java.util.TreeSet class of Java.

Syntax:

NavigableSet<K> java.util.TreeSet.headSet(K toElement, boolean inclusive)

This method takes two arguments. This method returns a view of the portion of this set whose elements are less than(or equal to, if inclusive is true) toElement.

Note: The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa.

Parameters: One parameter is required for this method.

toElement: high endpoint of the returned set.

inclusive: true if the high-end point is to be included in the returned view.

Returns: a view of the portion of this set whose elements are less than(or equal to, if inclusive is true) toElement.

Throws:

1. ClassCastException - if toElement is not compatible with this set's comparator.

2. NullPointerException - if toElement is null and this set uses natural ordering, or its comparator does not permit null elements.

3. IllegalArgumentException - if this set itself has a restricted range, and toElement lies outside the bounds of the range.

Approach 1: When no exception

Java

import java.util.TreeSet;

public class TreeSetheadSet2 {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        String toElement = "C++";
        boolean inclusive = true;

        System.out.println(treeSet.headSet(toElement,
inclusive));

    }
}

Output:

[C++]


Approach 2: NullPointerException

Java

import java.util.TreeSet;

public class TreeSetheadSet2 {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        String toElement = null;
        boolean inclusive = true;

        System.out.println(treeSet.headSet(toElement,
inclusive));

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Comparable.compareTo(Object)" because "k1" is null at java.base/java.util.TreeMap.compare(TreeMap.java:1563) at java.base/java.util.TreeMap$NavigableSubMap.<init>(TreeMap.java:1646) at java.base/java.util.TreeMap$AscendingSubMap.<init>(TreeMap.java:2129) at java.base/java.util.TreeMap.headMap(TreeMap.java:1196) at java.base/java.util.TreeSet.headSet(TreeSet.java:336)


TreeSet headSet(K) in Java

headSet(K): This method is available in java.util.TreeSet class of Java.

Syntax:

SortedSet<K> java.util.TreeSet.headSet(K toElement)

This method takes one argument. This method returns a view of the portion of this set whose elements are strictly less than toElement.

Note: The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa.

Parameters: One parameter is required for this method.

toElement: high endpoint (exclusive) of the returned set.

Returns: a view of the portion of this set whose elements are strictly less than toElement.

Throws:

1. ClassCastException - if toElement is not compatible with this set's comparator.

2. NullPointerException - if toElement is null and this set uses natural ordering, or its comparator does not permit null elements.

3. IllegalArgumentException - if this set itself has a restricted range, and toElement lies outside the bounds of the range

Approach 1: When no exception

Java

import java.util.TreeSet;

public class TreeSetheadSet {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        String toElement = "C++";

        System.out.println(treeSet.headSet(toElement));

    }
}

Output:

[]


Approach 2: NullPointerException 

Java

import java.util.TreeSet;

public class TreeSetheadSet {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        String toElement = null;

        System.out.println(treeSet.headSet(toElement));

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Comparable.compareTo(Object)" because "k1" is null at java.base/java.util.TreeMap.compare(TreeMap.java:1563) at java.base/java.util.TreeMap$NavigableSubMap.<init>(TreeMap.java:1646) at java.base/java.util.TreeMap$AscendingSubMap.<init>(TreeMap.java:2129) at java.base/java.util.TreeMap.headMap(TreeMap.java:1196) at java.base/java.util.TreeSet.headSet(TreeSet.java:336) at java.base/java.util.TreeSet.headSet(TreeSet.java:370)