TreeSet subSet(K, boolean, K, boolean) in Java

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

Syntax:

NavigableSet<K> java.util.TreeSet.subSet(K fromElement, boolean fromInclusive, K toElement, boolean toInclusive)

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

Note:

1. If fromElement and toElement are equal, the returned set is empty unless fromInclusive and toInclusive are both true.

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

Parameters: Four parameters are required for this method.

fromElement: low endpoint of the returned set.

fromInclusive: true if the low endpoint is to be included in the returned view.

toElement: high-end point of the returned set.

toInclusive: true if the high endpoint is to be included in the returned view.

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 toElementis 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 TreeSetsubSet2 {
    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";
        boolean fromInclusive = true, toInclusive = true;

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

    }
}

Output:

[C++, Hello, Java]


Approach 2: NullPointerException 

Java

import java.util.TreeSet;

public class TreeSetsubSet2 {
    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";
        boolean fromInclusive = true, toInclusive = true;

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

    }
}

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)


Approach 3: IllegalArgumentException 

Java

import java.util.TreeSet;

public class TreeSetsubSet2 {
    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++";
        boolean fromInclusive = true, toInclusive = true;

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

    }
}

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)


No comments:

Post a Comment