Question: Is there a way to call a method on a lambda expression?

Consider following example where the reversed() method is called on MileageComparator:
`Collections.sort(cars, new MileageComparator().reversed());`

If I write MileageComparator as a Lambda Expression with a reversed() call however, I get an error when writing
`Collections.sort(cars, ((Car car1, Car car2) -> (int) (car2.getMileage() - car1.getMileage())).reversed());`
while it works fine without reversed.

I do know that I can simply exchange car1 and car2 for the same result, I would however like to know if a method call on a lambda expression is possible in general.

Answer: 
The lambda expression you have can be associated with many types like `Comparator` of `Car` type as well as `BiFunction`, so compiler doesn't know how to interpret `reversed()` method which haven't assigned to type

```
  Comparator<Car> carComp = (Car car1, Car car2) -> (int) (car2.getMileage() - car1.getMileage());

  BiFunction<Car, Car, Integer> biFun = (Car car1, Car car2) -> (int) (car2.getMileage() - car1.getMileage());

```

From java-8 you can use static method [Comparator.comparing](https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#comparing-java.util.function.Function-) with method reference operator, [here](https://www.baeldung.com/java-8-comparator-comparing) with more examples 

```
Collections.sort(cars, Comparator.comparing(Car::getMileage).reversed());

```

So by using [Method References](https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html) operator you can create `Comparator` object that sorts based on `mileage`

```
Comparator<Car> carComp = Comparator.comparing(Car::getMileage);

```

And then reverse it by just calling `reversed` method

```
carComp.reversed();

```

You can also [sort](https://docs.oracle.com/javase/8/docs/api/java/util/List.html#sort-java.util.Comparator-) method on list from java-8

```
cars.sort(Comparator.comparing(Car::getMileage).reversed());

```
> 
> I would however like to know if a method call on a lambda expression is possible in general.
> 
> 
> 

It is not, because a lambda expression by itself is nothing.

The lambda expression is syntax sugar for implementing a functional interface. Without the interface defining the method signature, the lambda expression doesn't know what it is.

It's like creating an anonymous class using an undefined type name, e.g.

```
new Foo() {
    public void bar() {
        // This will not work
    }
}

```

That anonymous class will fail to compile if `Foo` doesn't exist.

Similarly, `(Car a) -> a.openDoor()` will fail to compile, because the compiler doesn't know what type is being implemented. To make it work, the compiler need to know which functional interface to implement, e.g.

```
Consumer<Car> x = (Car a) -> a.openDoor();

```

But the following will fail with error `Lambda expression needs an explicit target-type`:

```
var x = (Car a) -> a.openDoor();

```

When you wrap the lambda expression in parentheses and call a method on it, no functional interface can be inferred by the compiler, which is why this fails:

```
((Car a) -> a.openDoor()).accept(myCar)

```

You can however tell the compiler which interface to implement, by providing a cast:

```
((Consumer<Car>) (Car a) -> a.openDoor()).accept(myCar)

```