Inner Classes and Lambdas
Java 8 introduced lambda expressions to simplify programs.
lambda expression¶
An anonymous function that you can store in a variable or pass as an argument to a method.
A lambda's purpose is to implement the one method an inner class would implement, without the overhead of writing an entire inner class.
// Inner class
Comparator<Planet> comp = new Comparator<Planet>() {
@Override
public int compare(Planet a, Planet b) {
return a.getOrbit() > b.getOrbit() ? 1 : -1;
}
};
// Lambda
Comparator<Planet> compLambda = (Planet a, Planet b) -> {
return a.getOrbit() > b.getOrbit() ? 1 : -1;
};
A lambda is not a class, but it can replace certain inner classes.
Practice Exercise¶
Lambdas are a shift toward functional programming instead of object-oriented programming.
Functional Programming avoids changing-state and mutable (changeable) data.
Inputs are given to a function, it determines an output, and returns the output - but it does not change the input. The function simply calculates an output.