Java 8 features every developer must-have.

Java 8 features every developer must-have.

Even today, a Java developer is asked about the features of Java 8. But now, Oracle no longer provides free support for Java 8.  Hence, a Java developer must start with OpenJDK 11.

Lambda Expressions

This was the most anticipated feature of Java 8. Simply because of the fact that it made Java more closer to functional programming. Functional interfaces had been introduced in Java 8. The interface having just one abstract method is a functional interface. Lambda expressions enable Java’s object-oriented programming world with functional programming.

Functional Interfaces

From Java 8 onwards, we can simply use the instance of the functional interfaces using lambda expressions. A functional interface can have any number of default methods.

Traditionally interfaces used to have only abstract methods to achieve abstraction in Java. Abstract methods must be defined in the implemented classes. After Java 8 release, default methods are added to interfaces to add extra features to current interfaces without disrupting the current implementations.

Default Methods Can Cause The Diamond Problem

Abstract classes and interfaces can both contain abstract methods and implemented methods, although the syntax is different. Inheriting from different interfaces multiple default methods with the same signature and different implementations is a problem. The solution is simply to override the default method with its own implementation. Interfaces from Java 8 onwards can have static methods with implementation, too.

Interfaces and Java Stream API

List<String> allUsers = findAllUsers();

allUsers

    .stream()

    .filter(username - > username.startsWith('s'))

    .map(String::toUpperCase)

    .sorted()

    .forEach(System.out::println)

 A stream represents a sequence of elements and supports different kinds of operations to perform computations upon those elements. The operations are higher order functions, i.e. functions that take other functions as parameters, and are either intermediate or terminal operations. Intermediate operations return a stream, so we can chain multiple intermediate operations without using semicolons. In the example above, intermediate operations are filter, map, and sorted. Terminal operations are either void or return a non-stream result. In our simple example, forEach is a terminal operation. Other examples are reduce, collect, anyMatch, count, etc.

Interfaces from Java 8 onwards can have static methods with implementation. These methods can’t be overridden by the classes implementing the interface containing static methods. On the other hand, you can use Stream API to process collections of objects. It uses streams that can perform aggregate operations on data returned from collections and I/O operations. 

As shown in the two examples, counting collection using traditional methods iterates through the list, checks the condition, and then outputs the result. When counting the collection using streams, the stream method returns a stream of all the names, the filter method returns another stream of names with a length of more than 5, the count method reduces this stream to the result.

Optional Class

Java 8 has introduced a new class Optional which can help in writing a clean code without using too many null checks.


Reinhard Prehofer

When do you start to #jSparrow your Java Code ?

4 年

Streams startsa light paradigm shift towards functional ideas: => you rather specify what you want instead of programming how to get the results

回复
Georg Musil ?? Unternehmer. Coach. Multiplikator.

Partner TAB Wien - Ich organisiere systematischen Austausch von Unternehmern und Gesch?ftsführern und unterstütze sie dabei ihre Ziele zu erreichen.

4 年

Thank you for sharing, Michaela!

回复

要查看或添加评论,请登录

Michaela Prehofer的更多文章

社区洞察

其他会员也浏览了