Java TreeSet comparator() Method

The comparator() method of TreeSet class returns the comparator used for sorting the elements. If the comparator uses natural ordering for sorting, it returns null

Specified by

comparator in interface SortedSet<E> : it returns the comparator used for sorting the elements. If the comparator uses natural ordering for sorting, it returns null

Return:

It returns the comparator used for sorting the elements. It returns null in the case of natural ordering.

Example 1

Output:

TreeSet: null

Example 2

Output:

TreeSet: [A, B, a, b]