A lambda expression is a feature introduced in Java 8 that allows you to write short
and concise code. It's essentially a way to express an instance of a functional
interface (an interface with only one abstract method) in a more compact form.
Lambda expressions are often used for implementing the Comparator interface or
any functional interfaces.
In the example above, (int a, int b) represents the parameters, and a + b is the
expression that is executed when the lambda expression is invoked.
Uses:
Lambda expressions simplify the syntax for functional programming.
They're used for writing short blocks of code that can be passed around (like
sorting, filtering, or mapping).
It allows writing inline implementation of functional interfaces, like Runnable,
ActionListener, or Comparator.
Comparator in Java
The Comparator interface in Java is used to define a custom comparison
logic for objects. It allows sorting objects in an order other than their natural
order (which is defined by the Comparable interface).