TreeMap subMap(K, boolean, K, boolean) in Java

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

Syntax:

NavigableMap<K, V> java.util.TreeMap.subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)

This method takes four arguments. This method returns a view of the portion of this map whose keys range from fromKey to toKey. If fromKey and toKey are equal, the returned map is empty unless fromInclusive and toInclusive are both true.

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

Parameters: Four parameters are required for this method.

fromKey: low end point of the keys in the returned map.

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

toKey: high end point of the keys in the returned map.

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

Returns: a view of the portion of this map whose keys range from fromKey to toKey.

Throws:

1. ClassCastException - if fromKey and toKey cannot be compared to one another using this map's comparator.

2. NullPointerException - if fromKey or toKey is null and this map uses natural ordering, or its comparator does not permit null keys.

3. IllegalArgumentException - if fromKey is greater than toKey; or if this map itself has a restricted range, and fromKey or toKey lies outside the bounds of the range.

Approach 1: When no exception

Java

import java.util.TreeMap;

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

        TreeMap<Integer, String> treeMap =
new TreeMap<Integer, String>();

        treeMap.put(2, "Hello");
        treeMap.put(11, "Java");
        treeMap.put(23, "Program");
        treeMap.put(6, "C++");
        treeMap.put(25, "Program");

        Integer fromKey = 2, toKey = 23;
        boolean fromInclusive = true, toInclusive = true;

        System.out.println(treeMap.subMap(fromKey,
fromInclusive, toKey, toInclusive));

    }
}

Output:

{2=Hello, 6=C++, 11=Java, 23=Program}


Approach 2: NullPointerException 

Java

import java.util.TreeMap;

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

        TreeMap<Integer, String> treeMap =
new TreeMap<Integer, String>();

        treeMap.put(2, "Hello");
        treeMap.put(11, "Java");
        treeMap.put(23, "Program");
        treeMap.put(6, "C++");
        treeMap.put(25, "Program");

        Integer fromKey = null, toKey = 23;
        boolean fromInclusive = true, toInclusive = true;

        System.out.println(treeMap.subMap(fromKey,
fromInclusive, toKey, 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)


No comments:

Post a Comment