TreeSet comparator() in Java

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

Syntax:

Comparator<? super String> java.util.TreeSet.comparator()

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

Parameters: NA

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

Exceptions: NA

Approach

Java

import java.util.TreeSet;

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

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        System.out.println(treeSet.comparator());

    }
}

Output:

null


No comments:

Post a Comment