TreeSet subSet(K, K) in Java

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

Syntax:

SortedSet<K> java.util.TreeSet.subSet(K fromElement, K toElement)

This method takes two arguments. This method returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement,exclusive.

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

Parameters: Two parameters are required for this method.

fromElement: low endpoint (inclusive) of the returned set.

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

Returns: a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.

Throws:

1. ClassCastException - if fromElement and toElement cannot be compared to one another using this set's comparator.

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

3. IllegalArgumentException - if fromElement is greater than toElement; or if this set itself has a restricted range, and fromElement or toElement lies outside the bounds of the range

Approach 1: When no exception

Java

import java.util.TreeSet;

public class TreeSetsubSet {
    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 fromElement = "C++", toElement = "Java";

        System.out.println(treeSet.subSet(fromElement,
toElement));

    }
}

Output:

[C++, Hello]


Approach 2: NullPointerException

Java

import java.util.TreeSet;

public class TreeSetsubSet {
    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 fromElement = null, toElement = "Java";

        System.out.println(treeSet.subSet(fromElement,
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:1640) at java.base/java.util.TreeMap$AscendingSubMap.<init>(TreeMap.java:2129) at java.base/java.util.TreeMap.subMap(TreeMap.java:1182) at java.base/java.util.TreeSet.subSet(TreeSet.java:323) at java.base/java.util.TreeSet.subSet(TreeSet.java:359)



Approach 3: IllegalArgumentException

Java

import java.util.TreeSet;

public class TreeSetsubSet {
    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 fromElement = "Java", toElement = "C++";

        System.out.println(treeSet.subSet(fromElement,
toElement));

    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: fromKey > toKey at java.base/java.util.TreeMap$NavigableSubMap.<init>(TreeMap.java:1641) at java.base/java.util.TreeMap$AscendingSubMap.<init>(TreeMap.java:2129) at java.base/java.util.TreeMap.subMap(TreeMap.java:1182) at java.base/java.util.TreeSet.subSet(TreeSet.java:323) at java.base/java.util.TreeSet.subSet(TreeSet.java:359)


No comments:

Post a Comment