Comparator comparingInt() in Java with examples
The comparingInt(java.util.function.ToIntFunction) method accepts a function as parameter that extracts an int sort key from a type T, and returns a Comparator that compares by that sort key.The returned comparator is serializable if the specified function is also serializable.
static <T> Comparator<T> comparingInt(ToIntFunction <T> keyExtractor)
Parameters: This method accepts a single parameter keyExtractor which is the function used to extract the integer sort key.
Return value: This method returns a comparator that compares by an extracted key
Exception: This method throws NullPointerException if the argument is null.
Below programs illustrate comparingInt(java.util.function.ToIntFunction) method:
The output printed on console of IDE is shown below.
The output printed on console is shown below.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.