TreeMap remove(Object, Object) in Java

remove(Object, Object): This method is available in java.util.TreeMap class of Java.

Syntax:

boolean java.util.Map.remove(Object key, Object value)

This method takes two arguments. This method removes the entry for the specified key only if it is currently mapped to the specified value.

Parameters: Two parameters are required for this method.

key: key with which the specified value is associated.

value: value expected to be associated with the specified key.

Returns: true if the value was removed.

Throws:

1. UnsupportedOperationException - if the remove operation is not supported by this map.

2. ClassCastException - if the key or value is of an in appropriate type for this map.

3. NullPointerException - if the specified key or value is null,and this map does not permit null keys or values.

Approach 1: When no exception

Java

import java.util.TreeMap;

public class TreeMapremove2 {
    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");

        Object key = 11;

        Object value = "Java";

        System.out.println(treeMap.remove(key, value));

    }
}

Output:

true


Approach 2: NullPointerException

Java

import java.util.TreeMap;

public class TreeMapremove2 {
    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");

        Object key = null;

        Object value = "Java";

        System.out.println(treeMap.remove(key, value));

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.util.TreeMap.getEntry(TreeMap.java:345) at java.base/java.util.TreeMap.get(TreeMap.java:279) at java.base/java.util.Map.remove(Map.java:815)


TreeMap remove(Object) in Java

remove(Object): This method is available in java.util.TreeMap class of Java.

Syntax:

String java.util.TreeMap.remove(Object key)

This method takes one argument. This method removes the mapping for this key from this TreeMap if present.

Parameters: One parameter is required for this method.

key: key for which mapping should be removed.

Returns: the previous value associated with key, or null if there was no mapping for key.

Throws:

1. ClassCastException - if the specified key cannot be compared with the keys currently on the map.

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

Approach 1: When no exception

Java 

import java.util.TreeMap;

public class TreeMapremove {
    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");

        Object key = 11;

        System.out.println(treeMap.remove(key));

    }
}

Output:

Java


Approach 2: NullPointerException

Java

import java.util.TreeMap;

public class TreeMapremove {
    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");

        Object key = null;

        System.out.println(treeMap.remove(key));

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.util.TreeMap.getEntry(TreeMap.java:345) at java.base/java.util.TreeMap.remove(TreeMap.java:873)


TreeMap putIfAbsent(K, V) in Java

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

Syntax:

String java.util.TreeMap.putIfAbsent(K key, V value)

This method takes two arguments. If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.

Parameters: One parameter is required for this method.

key: key with which the specified value is to be associated.

value: value to be associated with the specified key.

Returns: the previous value associated with the specified key, or null if there was no mapping for the key.

Exceptions: NA

Approach

Java

import java.util.TreeMap;

public class TreeMapputIfAbsent {
    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 key = 10;

        String value = "World";

        System.out.println(treeMap.putIfAbsent(key, value));

    }
}

Output:

null


TreeMap putAll(Map) in Java

putAll(Map): This method is available in java.util.TreeMap class of Java.

Syntax:

void java.util.TreeMap.putAll(Map<? extends K, ? extends V> map)

This method takes one argument. This method copies all of the mappings from the specified map to this map. These mappings replace any mappings that this map had for any of the keys currently in the specified map.

Parameters: One parameter is required for this method.

map: mappings to be stored in this map.

Throws:

1. ClassCastException - if the class of a key or value in the specified map prevents it from being stored in this map.

2. NullPointerException - if the specified map is null or the specified map contains a null key and this map does not permit null keys

Approach 1: When no exception

Java

import java.util.TreeMap;

public class TreeMapputAll {
    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");

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

        treeMap2.putAll(treeMap);
        System.out.println(treeMap2);

    }
}

Output:

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


Approach 2: NullPointerException 

Java

import java.util.TreeMap;

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

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

        treeMap2.putAll(treeMap);
        System.out.println(treeMap2);

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Map.size()" because "map" is null at java.base/java.util.TreeMap.putAll(TreeMap.java:314)