Java Spliterator getComparator() Method

The getComparator() method of Java Interface Spliterator is used to get a Comparator if the given Spliterator?s source is SORTED by a Comparator. It returns null if the source is SORTED in a natural order. Otherwise, if the source is not SORTED, throws IllegalStateException.

Syntax

Following is the declaration of getComparator() method-

Parameter

This method does not accept any parameter.

Return

The getComparator() method would return a comparator or a null value if the elements were sorted in the natural order.

Exceptions

IllegalStateException- This method has thrown an exception if the spliterator does not report a characteristic of SORTED.

Compatibility Version

Java 1.8 and above

Example 1

Output:

Exception in thread "main" java.lang.IllegalStateException
	at java.base/java.util.Spliterator.getComparator(Spliterator.java:465)
	at myPackage.InterfaceSpliteratorGetComparatorExample1.main(InterfaceSpliteratorGetComparatorExample1.java:16)

Example 2

Output:

myPackage.InterfaceSpliteratorGetComparatorExample2$$Lambda$1/[email protected]
[Test{str='one'}, Test{str='two'}]