Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 stream max() function argument type Comparator vs Comparable

Tags:

I wrote some simple code like below. This class works fine without any errors.

public class Test {
    public static void main(String[] args) {
        List<Integer> intList = IntStream.of(1,2,3,4,5,6,7,8,9,10).boxed().collect(Collectors.toList());
        int value = intList.stream().max(Integer::compareTo).get();

        //int value = intList.stream().max(<Comparator<? super T> comparator type should pass here>).get();

        System.out.println("value :"+value);
    }
}

As the code comment shows the max() method should pass an argument of type Comparator<? super Integer>.

But Integer::compareTo implements Comparable interface - not Comparator.

public final class Integer extends Number implements Comparable<Integer> {
    public int compareTo(Integer anotherInteger) {
        return compare(this.value, anotherInteger.value);
    }
}

How can this work? The max() method says it needs a Comparator argument, but it works with Comparable argument.

I know I have misunderstood something, but I do now know what. Can someone please explain?

like image 678
Shalika Avatar asked Apr 15 '19 16:04

Shalika


People also ask

How to get max value from Stream in Java?

Stream. max() returns the maximum element of the stream based on the provided Comparator. A Comparator is a comparison function, which imposes a total ordering on some collection of objects. max() is a terminal operation which combines stream elements and returns a summary result.

What is Comparator in Java?

Java Comparator is an interface for sorting Java objects. Invoked by “java. util. comparator,” Java Comparator compares two Java objects in a “compare(Object 01, Object 02)” format. Using configurable methods, Java Comparator can compare objects to return an integer based on a positive, equal or negative comparison.


2 Answers

int value = intList.stream().max(Integer::compareTo).get();

The above snippet of code is logically equivalent to the following:

int value = intList.stream().max((a, b) -> a.compareTo(b)).get();

Which is also logically equivalent to the following:

int value = intList.stream().max(new Comparator<Integer>() {
    @Override
    public int compare(Integer a, Integer b) {
        return a.compareTo(b);
    }
}).get();

Comparator is a functional interface and can be used as a lambda or method reference, which is why your code compiles and executes successfully.

I recommend reading Oracle's tutorial on Method References (they use an example where two objects are compared) as well as the Java Language Specification on §15.13. Method Reference Expressions to understand why this works.

like image 185
Jacob G. Avatar answered Oct 06 '22 15:10

Jacob G.


I can relate to your confusion.

We've got a Comparator's method which declares two parameters

int compare(T o1, T o2);

and we've got an Integer's method which takes one parameter

int compareTo(Integer anotherInteger)

How on earth does Integer::compareTo get resolved to a Comparator instance?

When a method reference points to an instance method, the parser can look for methods with arity n-1 (n is the expected number of parameters).

Here's an excerpt from the JLS on how applicable methods are identified. I will drop the first part about parsing the expression preceding the :: token.

Second, given a targeted function type with n parameters, a set of potentially applicable methods is identified:

If the method reference expression has the form ReferenceType :: [TypeArguments] Identifier, then the potentially applicable methods are:

  • the member methods of the type to search that would be potentially applicable (§15.12.2.1) for a method invocation which names Identifier, has arity n, has type arguments TypeArguments, and appears in the same class as the method reference expression; plus

  • the member methods of the type to search that would be potentially applicable for a method invocation which names Identifier, has arity n-1, has type arguments TypeArguments, and appears in the same class as the method reference expression.

Two different arities, n and n-1, are considered, to account for the possibility that this form refers to either a static method or an instance method.

...

A method reference expression of the form ReferenceType :: [TypeArguments] Identifier can be interpreted in different ways. If Identifier refers to an instance method, then the implicit lambda expression has an extra parameter compared to if Identifier refers to a static method.

https://docs.oracle.com/javase/specs/jls/se12/html/jls-15.html#jls-15.13.1

If we were to write an implicit lambda expression from that method reference, the first (implicit) parameter would be an instance to call the method on, the second (explicit) parameter would be an argument to pass in the method.

(implicitParam, anotherInteger) -> implicitParam.compareTo(anotherInteger)

Note that a method reference differs from a lambda expression, even though the former can be easily transformed into the latter. A lambda expression needs to be desugared into a new method, while a method reference usually requires only loading a corresponding constant method handle.

Integer::compareTo implements Comparable interface - not Comparator.

Integer::compareTo as an expression doesn't implement any interface. However, it can refer to/represent different functional types, one of which is Comparator<Integer>.

Comparator<Integer> a = Integer::compareTo;
BiFunction<Integer, Integer, Integer> b = Integer::compareTo;
ToIntBiFunction<Integer, Integer> c = Integer::compareTo;
like image 27
Andrew Tobilko Avatar answered Oct 06 '22 17:10

Andrew Tobilko