TreeMap comparator() in Java

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

Syntax:

Comparator<? super Integer> java.util.TreeMap.comparator()

This method returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.

Parameters: NA

Returns: the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.

Exceptions: NA

Approach

Java

import java.util.TreeMap;

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

        System.out.println(treeMap.comparator());
    }
}

Output:

null


No comments:

Post a Comment