Successfully reported this slideshow.
Your SlideShare is downloading. ×

Functional Interfaces and Method References.pptx

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 15 Ad

More Related Content

Recently uploaded (20)

Advertisement

Functional Interfaces and Method References.pptx

  1. 1. Reactive Systems Functional Interfaces & Method References MIKE.CONNOLLY@LIT.IE
  2. 2. Java Lambdas What we will cover •java.util.function Interfaces •Method References
  3. 3. Functional Interfaces •What is the type of a lambda expression? • We have used an interface with a single abstract method to represent a method • We call this kind of interface a functional interface •We have seen Functional Interfaces and how to create them • If we had ready made lambda types (like Strings or Doubles) we would reduce our code even more •Java introduced lots of ready made function Interfaces for just this purpose!
  4. 4. Java.util.function •Rather than creating an interface for each specific lambda we want to create Java provide a library of ready to go lambda interfaces • https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html We will now look at Function<>, Predicate<> and Consumer<> Function Interfaces
  5. 5. Java.util.function.Function •Function<T,R> Represents a function that accepts one argument of type T and produces a result of type R •The apply(T t) method applies this function to the given argument.
  6. 6. Java.util.function.Predicate •Predicate<T> Represents a predicate (boolean-valued function) of one argument. •The test(T t) method evaluates this predicate on the given argument.
  7. 7. Java.util.function.Predicate •or(Predicate<? super T> other) Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another and(Predicate<? super T> other) Returns a composed predicate that represents a short- circuiting logical AND of this predicate and another negate() Returns a predicate that represents the logical negation of this predicate.
  8. 8. Java.util.function.BiFunction •BiFunction<T,U,R> •Represents a function that accepts two arguments and produces a result • T - the type of the first argument to the function • U - the type of the second argument to the function • R - the type of the result of the function •apply(T t, U u) • Applies this function to the given arguments.
  9. 9. Java.util.function.Consumer •Consumer<T> •Represents an operation that accepts a single input argument and returns no result. •accept(T t) • Performs this operation on the given argument. • Returns void
  10. 10. Functional Composition •The andThen() method can be used to compose new functions from existing ones. •It creates a new Function from the Function that called andThen() and the Function passed to the andThen() •A Function composed with andThen() will first call the Function that called andThen(), then it will call the Function passed to andThen(). •Consumer & BiFunction also have andThen() methods.
  11. 11. Java Method References •We use lambda expressions to create anonymous methods •Sometimes lambdas only call an existing method and do nothing else •It is easier to understand the code if we refer to the existing method by name •Method references are compact, easy-to-read lambda expressions for methods that already have a name •References: •https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html •Java Brains - https://youtu.be/lwwIZuwYmNI
  12. 12. Java Method References Kind Syntax Examples Reference to a static method ContainingClass::staticMethodName Person::compareByAge Reference to an instance method of a particular object containingObject::instanceMethodName myApp::appendStrings2 Reference to an instance method of an arbitrary object of a particular type ContainingType::methodName String::compareToIgnoreCase Reference to a constructor ClassName::new HashSet::new
  13. 13. Java Method References Example •Thread accepts a Runnable as an argument. Runnable is a functional interface whose single abstract method is run (zero parameters and void return type) •When an instance of Thread calls it’s start method, the Runnable run method runs in a new thread of execution. •Here we create a lambda that just calls the System.out.println() function •We see that the method reference in thread2 has the same behaviour as calling the method in thread1
  14. 14. Java Method References Example •Here we create a list of Car objects and a Predicate similar to earlier examples • The perform conditionally method accepts a list a predicate and a Consumer • If any element from the list satisfies the Predicate the Consumer operates on it. •In this case our Consumer is defined, using a method reference, as the block of code associated with System.out.println

×