Collections.checkedNavigableMap(NavigableMap, Class, Class) in Java

Collections.checkedNavigableMap(NavigableMap, Class, Class): This method is available in java.util.Collections class of Java.

Syntax:

<E1, E2> NavigableMap<E1, E2> java.util.Collections.checkedNavigableMap(NavigableMap<E1, E2> m, Class<E1> keyType, Class<E2> valueType)

This method takes three arguments. This method returns a dynamically typesafe view of the specified navigable map.

Note: Any attempt to insert a mapping whose key or value have the wrong type will result in an immediate ClassCastException.

Parameters: Three parameters are required for this method.

m: the map for which a dynamical type safe view is to be returned.

keyType: the type of key that m is permitted to hold.

valueType: the type of value that m is permitted to hold.

Returns: a dynamically typesafe view of the specified map.

Exceptions: NA

Approach

Java

import java.util.Collections;
import java.util.NavigableMap;
import java.util.TreeMap;

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

        NavigableMap<String, Integer> map = new TreeMap<String,
Integer>();
        map.put("Hello", 23);
        map.put("World", 11);
        System.out.println(Collections.checkedNavigableMap(map,
String.class, Integer.class));

    }
}

Output:

{Hello=23, World=11}


No comments:

Post a Comment