Содержание
- 2. Agenda Anonymous classes Default methods in interfaces Lambda expressions Functional Interfaces Method and Constructor References Lambda
- 3. Anonymous Inner Classes Anonymous inner class – class that has no name and is used if
- 4. people.sort( new Comparator () { @Override public int compare(Person p1, Person p2) { return p1.getName().compareTo(p2.getName()); }
- 5. Default Methods for Interfaces Java 8 enables us to add non-abstract method implementations to interfaces by
- 6. Default Methods for Interfaces Besides the abstract method calculate the interface Formula also defines the default
- 7. Default Methods for Interfaces The formula is implemented as an anonymous object. As we'll see in
- 8. Private methods for Interfaces From Java SE 9 on-wards, we can write private and private static
- 9. Lambda expressions Sort a list of strings in prior versions of Java: List names = Arrays
- 10. Lambda expressions The static utility method Collections.sort accepts a list and a comparator in order to
- 11. Lambda expressions In Java 8 comes with a much shorter syntax, lambda expressions Collections.sort(names, (String a,
- 12. Lambda expressions As you can see the code is much shorter and easier to read. But
- 13. Functional Interfaces How does lambda expressions fit into Javas type system? Each lambda corresponds to a
- 14. Functional Interfaces For example, Keep in mind that the code is also valid if the @FunctionalInterface
- 15. Method and Constructor References The above example code can be further simplified by utilizing static method
- 16. Method and Constructor References We can also reference instance methods: class StringUtil { char startsWith(String s)
- 17. Method and Constructor References Let's see how the :: expression works for constructors. class Person {
- 18. Method and Constructor References Next we specify a person factory interface to be used for creating
- 19. Lambda Scopes Accessing outer scope variables from lambda expressions is very similar to anonymous objects. You
- 20. Accessing local variables We can read final local variables from outer scope of lambda expressions: As
- 21. Accessing fields and static variables We also have both read and write access to instance fields
- 22. Accessing Default Interface Methods Interface Formula defines a default method sqrt which can be accessed from
- 23. Built-in Functional Interfaces The JDK 1.8 API contains many built-in functional interfaces. Some of them are
- 24. Predicates Predicates are boolean-valued functions of one argument. The interface contains various default methods for composing
- 25. Functions Functions accept one argument and produce result. Default methods can be used to chain multiple
- 26. Suppliers Suppliers produce a result of a given generic type. Unlike Functions, Suppliers don't accept arguments.
- 27. Consumers Consumers represents operations to be performed on a single input argument. public interface Consumer {
- 28. Optionals Optionals are not functional interfaces, instead it's a nifty utility to prevent NullPointerException. Optional is
- 30. Скачать презентацию