AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
![]() ![]() ![]() More formally put: Functional interfaces provide target types for lambda expressions and method references. Where an instance of such an interface is required, a Lambda Expression can be used instead. Q6) What Is a Functional Interface? What Are the Rules of Defining a Functional Interface?Ī functional interface is an interface with no more, no less but one single abstract method (default methods do not count). ![]() UnaryOperator – it is similar to a Function, taking a single argument and returning a result of the same typeįor more on functional interfaces, see the article “ Functional Interfaces in Java 8”. The two arguments and the result are all of the same types Predicate – it takes one argument and returns a booleanīiFunction – it takes two arguments and returns a resultīinaryOperator – it is similar to a BiFunction, taking two arguments and returning a result. Supplier – it takes no argument and returns a result There are a lot of functional interfaces in the package, the more common ones include but not limited to:įunction – it takes one argument and returns a resultĬonsumer – it takes one argument and returns no result (represents a side effect) Q5) Describe Some of the Functional Interfaces in the Standard Library. It is not recommended to be used as a field value in entity classes, which is clearly indicated by it not implementing the Serializable interface. It's worth noting that Optional is not a general-purpose class like Option in Scala. This allows us to easily handle such case: But what if the stream is empty? If it was not for Optional, the method would return null or throw an exception.īut it returns an Optional value which may be Optional.empty() (the second case). In Java 8, an Optional return type explicitly requires you to handle null or non-null wrapped values differently.įor instance, the Stream.max() method calculates the minimum value in a stream of values. Such methods would require you to write boilerplate code to check the return value and sometimes could forget to do a defensive check. The main purpose of Optional, as designed by its creators, was to be a return type of methods that previously would return null. You can read a dedicated article about Optional here. Thus it can be used instead of a nullable value to get rid of NullPointerException in many cases. Optional has a special Optional.empty() value instead of wrapped null. It is a wrapper around an object, and you can think of it as a container of zero or one element. Optional is a new class in Java 8 that encapsulates an optional value i.e. It is a static method reference to the valueOf method of the String class. Q3) What Is the Meaning of String::valueOf Expression? They only work as syntactic sugar to reduce the verbosity of some lambdas. It is used for treating methods as Lambda Expressions. Stream API − a special iterator class that allows processing collections of objects in a functional mannerĭate API − an improved, immutable JodaTime-inspired Date APIĪlong with these new features, lots of feature enhancements are done under-the-hood, at both compiler and JVM level.Ī method reference is a Java 8 construct that can be used for referencing a method without invoking it. Nashorn, JavaScript Engine − Java-based engine for executing and evaluating JavaScript code Optional − special wrapper class used for expressing optionalityįunctional Interface – an interface with maximum one abstract method, implementation can be provided using a Lambda Expressionĭefault methods − give us the ability to add full implementations in interfaces besides abstract methods Method References − enable defining Lambda Expressions by referring to methods directly using their names Lambda Expressions − a new language feature allowing treating actions as objects Java 8 ships with several new features but the most significant are the following and will be covering the interview questions on these areas. Q1) What Are The New Features Were Added in Java 8? ![]()
0 Comments
Read More
Leave a Reply. |