This is the 8th day of my participation in the First Challenge 2022
preface
The current mainstream version of development we use is Java 8, which is a long-supported version, so it’s important to take a look at the features of the Java 8JDK.
Java 8
The major changes in the Java 8 release are as follows:
- Lambda Expression and Stream API (Lambda Expression and Stream API)
- Method Reference
- Default Methods
- Type Annotations
- Repeating Annotations
- Method Parameter Reflection
We’ll cover the first two features today:
Lambda Expression and Stream API (Lambda Expression and Stream API)
Java has long been known for having a lot of boilerplate code. With the release of Java 8, this is becoming less definitive. Stream apis and lambda expressions are new features that bring us closer to functional programming. In our example, we’ll see how lambda and streams can be used in different scenarios.
Previous use of Lambda expressions
We have a car dealership business. To get rid of all the paperwork, we wanted to create software to find all the currently available cars with less than 50,000 kilometers. Let’s look at how we can implement this functionality in a naive way:
public class LambdaExpressions {
public static List<Car> findCarsOldWay(List<Car> cars) {
List<Car> selectedCars = new ArrayList<>();
for (Car car : cars) {
if (car.kilometers < 50000) { selectedCars.add(car); }}returnselectedCars; }}Copy the code
To do this, we are creating a static function that accepts a list of cars. It should return a filter list based on the specified criteria.
Use streams and Lambda expressions
We used the same problem as in the previous example. Our customers want to find all cars with the same standard. Let’s look at a solution that uses the stream API and lambda expressions:
public class LambdaExpressions {
public static List<Car> findCarsUsingLambda(List<Car> cars) {
return cars.stream().filter(car -> car.kilometers < 50000) .collect(Collectors.toList()); }}Copy the code
We need to transfer the list of cars to the stream by calling the stream() method. In the filter() method, we are setting our criteria. We are evaluating each item against the requirements. We only keep entries with less than 50,000 km. The last thing we need to do is wrap it up as a list.
Method Reference
No method references are used
We still own a car dealership, and we want to print all the cars in the store. To do this, we will use method references. Method references allow us to use a special syntax :: to call a function in a class. There are four ways to cite:
- Reference static methods
- Reference instance methods on objects
- Reference instance methods on types
- A reference to the constructor
We use lambda expressions to call the toString() method on each car.
public class MethodReference {
List<String> withoutMethodReference =
cars.stream().map(car -> car.toString())
.collect(Collectors.toList());
}
Copy the code
Use method reference
Now, let’s see how to use method references in the same situation:
public class MethodReference {
List<String> methodReference = cars.stream().map(Car::toString)
.collect(Collectors.toList());
}
Copy the code
Again, we use lambda expressions, but now we call the toString() method through a method reference. We can see how it is much cleaner and easier to read.
conclusion
It’s important to be good at optimizing your code with new features, so be aware of new features
reference
(reflectoring. IO/Java – releas…).