Javatpoint Logo
Javatpoint Logo

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

Test it Now

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

Test it Now

Output:

myPackage.InterfaceSpliteratorGetComparatorExample2$$Lambda$1/1639622804@18e8568
[Test{str='one'}, Test{str='two'}]



Help Others, Please Share

facebook twitter pinterest