Java Reference
In-Depth Information
Java SE 8: @FunctionalInterface Annotation
You can create your own functional interfaces by ensuring that each contains only one
abstract method and zero or more default or static methods. Though not required,
you can declare that an interface is a functional interface by preceding it with the @Func-
tionalInterface annotation . The compiler will then ensure that the interface contains
only one abstract method; otherwise, it'll generate a compilation error.
17.11 Java SE 8 and Functional Programming Resources
Check out the topic's web page at
http://www.deitel.com/books/jhtp10
for links to the online Deitel Resource Centers that we built as we were writing Java How
to Program, 10/e .
17.12 Wrap-Up
In this chapter, you learned about Java SE 8's new functional programming capabilities.
We presented many examples, often showing simpler ways to implement tasks that you
programmed in earlier chapters.
We overviewed the key functional programming technologies—functional interfaces,
lambdas and streams. You learned how to process elements in an IntStream —a stream of
int values. You created an IntStream from an array of int s, then used intermediate and
terminal stream operations to create and process a stream pipeline that produced a result.
You used lambdas to create anonymous methods that implemented functional interfaces.
We showed how to use a forEach terminal operation to perform an operation on each
stream element. We used reduction operations to count the number of stream elements,
determine the minimum and maximum values, and sum and average the values. You also
learned how to use method reduce to create your own reduction operations.
You used intermediate operations to filter elements that matched a predicate and map
elements to new values—in each case, these operations produced intermediate streams on
which you could perform additional processing. You also learned how to sort elements in
ascending and descending order and how to sort objects by multiple fields.
We demonstrated how to store the results of a stream pipeline into a collection for
later use. To do so, you took advantage of various prededined Collector implementations
provided by class Collectors . You also learned how to use a Collector to group elements
into categories.
You learned that various Java SE 8 classes have been enhanced to support functional
programming. You then used Files method lines to get a Stream<String> that read
lines of text from a file and used SecureRandom method ints to get an IntStream of
random values. You also learned how to convert an IntStream into a Stream<Integer>
(via method boxed ) so that you could use Stream method collect to summarize the fre-
quencies of the Integer values and store the results in a Map .
Next, you learned how to implement an event-handling functional interface using a
lambda. Finally, we presented some additional information about Java SE 8 interfaces and
streams. In the next chapter, we discuss recursive programming in which methods call
themselves either directly or indirectly.
 
 
 
Search WWH ::




Custom Search