Introduction
Oracle has made a great move by delivering several new features in Java to let developers create a clean, concise and an effective code and be much more productive. This release didn't touch only Java as language but also its libraries, compile and even the Java Virtual Machine. That is why experts all over the world think that this release is the biggest Java revolution since the fifth release in 2004.
Background
So, there are several new features like the lambdas expression, the newly Stream
API, the concurrency, the parallel arrays, the Date/Time API and others.
Personally, I think that the most 3 important features are the Stream API, Lambdas Expression and the Date/Time API.
Using the Code
Let us start with the Stream
API which is the most important feature at all. With it, you are not obliged to use the loops and thanks to it, you have a more flexible solution.
This API makes simple processing collections and allows you to make more readable code.
You can understand the difference between the new feature and the traditional loops in this simple example:
public Product getFirstFredProduct()
{
for (Product p : products)
{
if (p.getSellers().contains("Fred"))
{
return p ;
}
}
return null ;
}
Let us take a look at the new Stream
API feature:
public Optional<Product> getFirstFredProduct1()
{
return products.stream()
.filter(product->product->getSellers().contains("Fred"))
.findFirst();
}
Now, let us move to the lambdas expression that every functional developer is very familiar with. Lamdas Expression or the best known as Closures is a concept that allows us to pass functions around and treat code as data like the example as below.
Arrays.asList( "Sfax", "Tunisia",
"Tokyo","Japan" ).forEach( ( String e ) -> System.out.println( e ) );
Added to that, Date
and Time
are concerned by the new release of Java. There are many improvements in this API that its package contains all the classes for date, time, time zones, clock manipulations…
For example, we can get the local date and local time in this easy way:
final LocalTime MyLocalTime = LocalTime.now();
final LocalTime ThetimeFromClock = LocalTime.now( clock );