The article is true in the sense that you cannot sort based on an object of type BiFunction , but you can always use Comparator . But hey, they both can have the same body. For instance:
private static void sort(Comparator<String> ls){ Arrays.sort(someArray, ls); } Comparator<String> comp = (String f1, String f2) -> Integer.compare(f1.length(), f2.length()); sort(comp); BiFunction<String, String, Integer> func = (String f1, String f2) -> Integer.compare(f1.length(), f2.length()); sort((String f1, String f2) -> Integer.compare(f1.length(), f2.length()));
On line 4, you can pass in a lambda that is exactly the same as func . But you still cannot go through func to sort . Lambdas in java8 is an implementation of some FunctionalInterface . Functional interfaces get their type based on its reference type. The way the same lambda during initialization can be either BiFunction or Comparator .
But once the lambda is built and it gets its type, then you cannot change it. Therefore, you cannot pass a func type BiFunction to the sort that Comparator expects
source share