Inner Class in Java
Inner Class in Java
http://viewer.books24x7.com/AssetViewer.aspx?...
1 of 15
13/10/2016 10:25
http://viewer.books24x7.com/AssetViewer.aspx?...
In this chapter, the features of the first two parts of the specificationPart A:
Functional Interfaces and Part B: Lambda Expressionsare examined, as the
content of these parts are included on the exam. After reviewing Parts A and B of
the specification, a working example of lambda expressions and the Predicate FI
are presented.
Functional Programming
Functional programming is integrated with Java in Java SE 8. Lambda expressions
support functional programming in Java and add several benefits that software
developers can take advantage of, including ease of use, one of the most important
features. Lambdas in Java have long been awaited and their absence criticized.
They delightfully provide an alternative to the awkward and difficult-to-understand
anonymous inner classes that routinely challenge new developers. In addition to
ease of use, lambda expressions offer other values brought from the introduction of
functional programming:
q A clear and concise way to represent a method interface using an expression
q Code reduction, typically to a single line
q Improvement to the collections library (easier iteration, filtering, sorting, counting, data
extraction, and so on)
q Improvement to the concurrency library (improved performance in multicore environments)
q Lazy evaluation support through streams
Larger View
Functional Interfaces
Lambda expressions must have a functional interface, also called a single abstract
method (SAM). FIs provide target types for lambda expressions as well as method
references. FIs must also have exactly one abstract method. Because the name of
the method is known, the method name is excluded from the actual lambda
expression. This concept is explored in the upcoming syntax and example sections.
An FI may have one or more default methods and one or more static methods.
Default methods allow for the addition of new code to the interface, ensuring
backward compatibility. This ensures that legacy code that implements the interface
2 of 15
13/10/2016 10:25
http://viewer.books24x7.com/AssetViewer.aspx?...
will not break if the new default methods are not used. (Note that default and static
methods in the context of FIs are not on the exam.) An example of an FI that also
includes default methods is Javas Predicate FI:
Larger View
Larger View
The Predicate FI is one of many general-purpose FIs, which are included in the
java.util.function package for the primary use of features of the JDK.
General-purpose FIs are listed in Table H-2 in Appendix H. The Predicate FI will
be visited later in the chapter, because you will see this FI on the exam.
The Java SE 8 API has several specific-purpose functional interfaces as well, and
the number will continue to grow as the API is extended and refined. Various
specific-purpose FIs are listed in Table H-1 of Appendix H.
Instances of functional interfaces can be created with method references,
constructor references, and lambda expressions.
FIs should be annotated with the @FunctionalInterface annotation to aid the
compiler in checking for specification adherence and the IDE in features support.
An example of IDE features support would be refactoring of anonymous inner
classes to lambda expressions.
Lambda Expressions
Lambda expressions allow for the creation and use of single method classes.
These methods have a basic syntax that provides for the omission of modifiers, the
return type, and optional parameters. Full syntax includes a parameter list with
explicit or implicit target types, the arrow operator, and the body with statements. A
target type is the type of object in which a lambda is bound. Multiple target types
must be enclosed in parentheses. A body with multiple statements must be
enclosed in braces. Therefore, syntax for lambda expressions is either of these:
Larger View
3 of 15
13/10/2016 10:25
http://viewer.books24x7.com/AssetViewer.aspx?...
Larger View
Larger View
4 of 15
13/10/2016 10:25
http://viewer.books24x7.com/AssetViewer.aspx?...
Larger View
Larger View
5 of 15
13/10/2016 10:25
http://viewer.books24x7.com/AssetViewer.aspx?...
Lets look at a simple program that exercises the sort and use of the Water transfer
object. Here, we build an array list of Water objects and then perform an
alphabetical sort on them by instantiating the WaterSort class that implements
the Comparator interface.
Larger View
Larger View
6 of 15
13/10/2016 10:25
http://viewer.books24x7.com/AssetViewer.aspx?...
Larger View
None of these specific details of the Comparator FI will be on the exam, but it is
good to see how an FI can be constructed and that it follows the rules, as shown in
the next code listing. If any of the rules are broken in the coding of the interface,
compiler errors will be thrown, thanks to the inclusion of the
@FunctionalInterface annotation.
7 of 15
13/10/2016 10:25
http://viewer.books24x7.com/AssetViewer.aspx?...
Larger View
Larger View
Larger View
8 of 15
13/10/2016 10:25
http://viewer.books24x7.com/AssetViewer.aspx?...
Larger View
Java SE 8 aims to solve the vertical issue of several lines of code with a simpler
expressive means with lambda expressions. Thus, the six lines of code shown in
the anonymous inner class example can be replaced with the following abbreviated
code statement using the Comparator FI in the context of a lambda expression:
Larger View
Larger View
Also, since the target types are known, the expression can be further simplified by
removing the class names, so (Water w1, Water w2) would be (w1, w2).
Larger View
9 of 15
13/10/2016 10:25
http://viewer.books24x7.com/AssetViewer.aspx?...
Simpler even (and you can probably use this on the job right away) is that the
Comparator class provides a comparingWith method that accepts a lambda
expression. The return value of this method can be used with the sort method of
various collection types (such as ArrayList):
Larger View
Three classes are provided here that will collectively help demonstrate the use of
lambda expressions through five similar scenarios:
q PlanetApp Application that creates planet objects and queries filtered planet lists using the
Predicate FI
q Planet A value/transfer object representing planets as basic Java Beans
q PlanetPredicates A helper class making use of predicates
Lets list the Planet class first, because its just a simple bean showing a Planet
object with state representing their names, primary color, number of moons, and
whether or not the planets have rings.
Larger View
Larger View
10 of 15
13/10/2016 10:25
http://viewer.books24x7.com/AssetViewer.aspx?...
Larger View
Now, lets look at the source code for the application. Well step through each
scenario that uses lambda expressions, one at a time. Note that the scenarios
provide standard output listings where the Predicate FI has been used to assist
with the task. The five scenarios are listed here from a general user goal
perspective, followed by the overall source code, and then a general description of
each scenario.
q
q
q
q
q
Larger View
11 of 15
13/10/2016 10:25
http://viewer.books24x7.com/AssetViewer.aspx?...
Larger View
Larger View
Larger View
12 of 15
13/10/2016 10:25
http://viewer.books24x7.com/AssetViewer.aspx?...
Here the method is called, accepting the Predicate type in the second argument:
Larger View
Notice that replacing the enhanced for loop with a pipeline is beneficial, and in
most cases, your IDE can do it for you.
Larger View
Larger View
Larger View
13 of 15
13/10/2016 10:25
http://viewer.books24x7.com/AssetViewer.aspx?...
Larger View
Larger View
Larger View
Larger View
As you should be deeply familiar with the syntax by now, youll know that the
lambda could have been abbreviated even further:
Larger View
14 of 15
13/10/2016 10:25
http://viewer.books24x7.com/AssetViewer.aspx?...
2. Create a new JavaFX project (choose File | New Project, select JavaFX,
select JavaFXApplication, click Next, and click Finish). A JavaFX application
will be created that includes an anonymous inner class.
4. Click the light bulb in the glyph gutter at line 26. Notice that if you hover the
mouse over the light bulb, you will see this message: This anonymous inner
class creation can be turned into a lambda expression.
15 of 15
13/10/2016 10:25