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());

```
A couple of points.

You tried this and it didn't work.

```
       Collections.sort(cars, ((Car car1, Car car2) -> (int) (car2.getMileage() - 
                car1.getMileage())).reversed())

```

The Comparator is the same as the following:

```
       Comparator<Car> comp = ((car1, car2)->car1.getMileage() - car2.getMileage()).reversed();

```

Which won't compile either because the lambda on the right knows nothing about the `reversed()` method of the `Comparator`.

But you could have done this:

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

```

And then

```
       Collections.sort(cars, comp.reversed());

```

Because `comp` is a Comparator and knows about the `reversed()` method.

One final point. It is not a good idea to use `(a,b)-> a - b` because subtracting values will not always yield the expected results. It is better form to do `(a,b)-> (a > b) ? 1 : (a < b) ? -1 : 0`