I have recently started to tinker with java's method reference operator ( :: ) and there is something i can't understand.
Comparator< T > functional interface can be implemented with a method that has two paremeters of T type and returns an int. For example:
Comparator< String > stringComparator = (param1, param2) -> 0;
The String class has several methods to perform comparison. Among them, we can find these two:
int compareTo​(String anotherString)
int compareToIgnoreCase​(String str)
Those two methods have only one parameter. So, if they have one one parameter, why is it that this works?
Comparator< String > a = String::compareToIgnoreCase;
Comparator< String > b = String::compareTo;
Collections.sort, expecting a instance of comparator, also allow this (which is, if you think about it, the same case).
List<String> strings = Arrays.asList("zfds","jgdsf", "qgkp","cjy","mhy");
Collections.sort(strings, String::compareToIgnoreCase);
Collections.sort(strings, String::compareTo);
Why does this work? Why those two methods, which have only one parameter, are allowed to implement the compare method of the Comparator interface if this method has two paremeters intead of one?
Lots of thanks.