Java 8 Training – Class 2
-Marut Singh
Email: singh.marut@gmail.com
http://www.marutsingh.com/
http://marutsingh.com/
Built-in Functional Interfaces
Predicate Interface
@FunctionalInterface
public interface Predicate<T> {
* Evaluates this predicate on the given argument.
boolean test(T t);
default Predicate<T> and(Predicate<? super T> other) {
default Predicate<T> negate()
default Predicate<T> or(Predicate<? super T> other)
static <T> Predicate<T> isEqual(Object targetRef)
}
http://marutsingh.com/
Built-in Functional Interfaces - Quiz
 If functional interfaces can have only one abstract method then how does
Predicate qualify for functional interface?
http://marutsingh.com/
Built-in Functional Interfaces
Consumer Interface
accepts a single argument by calling its accept (args) method and does not return any
value making it a void method
Unlike most other functional interfaces, Consumer is expected to operate via side-
effects.
@FunctionalInterface
public interface Consumer<T> {
* Performs this operation on the given argument.
void accept(T t);
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
http://marutsingh.com/
Built-in Functional Interfaces
Supplier Interface
A supplier of objects. The result objects are either created during the invocation
of get() or by some prior action.
The supplier does the opposite of the consumer, so it takes no arguments but it
returns some value by calling its get() method.
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
Example:
Supplier<String> i = ()-> "springpeople.com";
System.out.println(i.get());
http://marutsingh.com/
Built-in Functional Interfaces
 Function
http://marutsingh.com/
Pipelining
 Stream operations return stream itself so that their result can be pipelined.
These operations are called intermediate operations and their function is to
take input, process them, and return output to the target. collect() method is
a terminal operation which is normally present at the end of the pipelining
operation to mark the end of the stream
http://marutsingh.com/
Stream pipelining
http://marutsingh.com/
Binary versions of base interfaces
http://marutsingh.com/
Lambda Operations
 Extracting data from an object using map
 Stream Operations
 Optional Class
 Lazy Processing
 Sorting
 Collect Method
 Grouping and Partition
http://marutsingh.com/

Java8 training - class 2

  • 1.
    Java 8 Training– Class 2 -Marut Singh Email: singh.marut@gmail.com http://www.marutsingh.com/ http://marutsingh.com/
  • 2.
    Built-in Functional Interfaces PredicateInterface @FunctionalInterface public interface Predicate<T> { * Evaluates this predicate on the given argument. boolean test(T t); default Predicate<T> and(Predicate<? super T> other) { default Predicate<T> negate() default Predicate<T> or(Predicate<? super T> other) static <T> Predicate<T> isEqual(Object targetRef) } http://marutsingh.com/
  • 3.
    Built-in Functional Interfaces- Quiz  If functional interfaces can have only one abstract method then how does Predicate qualify for functional interface? http://marutsingh.com/
  • 4.
    Built-in Functional Interfaces ConsumerInterface accepts a single argument by calling its accept (args) method and does not return any value making it a void method Unlike most other functional interfaces, Consumer is expected to operate via side- effects. @FunctionalInterface public interface Consumer<T> { * Performs this operation on the given argument. void accept(T t); default Consumer<T> andThen(Consumer<? super T> after) { Objects.requireNonNull(after); return (T t) -> { accept(t); after.accept(t); }; } } http://marutsingh.com/
  • 5.
    Built-in Functional Interfaces SupplierInterface A supplier of objects. The result objects are either created during the invocation of get() or by some prior action. The supplier does the opposite of the consumer, so it takes no arguments but it returns some value by calling its get() method. @FunctionalInterface public interface Supplier<T> { /** * Gets a result. * * @return a result */ T get(); } Example: Supplier<String> i = ()-> "springpeople.com"; System.out.println(i.get()); http://marutsingh.com/
  • 6.
    Built-in Functional Interfaces Function http://marutsingh.com/
  • 7.
    Pipelining  Stream operationsreturn stream itself so that their result can be pipelined. These operations are called intermediate operations and their function is to take input, process them, and return output to the target. collect() method is a terminal operation which is normally present at the end of the pipelining operation to mark the end of the stream http://marutsingh.com/
  • 8.
  • 9.
    Binary versions ofbase interfaces http://marutsingh.com/
  • 10.
    Lambda Operations  Extractingdata from an object using map  Stream Operations  Optional Class  Lazy Processing  Sorting  Collect Method  Grouping and Partition http://marutsingh.com/