Chapter 2. Using Built-in Lambda Types

2.1.  Describe the interfaces of the java.util.function package

[Note]

There are a lot of re-usable functional requirements that can be captured by functional interfaces and lambdas. The designers of Java 8 have captured the common use cases and created a library of functions for them. A new package called java.util.function was created to host these common functions.

[Warning]

Listings of the interfaces provided in this section are not complete. The listings only show the most important parts of the source code - signatures of the functional methods.

java.util.function.Predicate

  • Parameter type: T

  • Return type: boolean

  • Description: Represents a predicate (boolean-valued function) of one argument.


package java.util.function;

@FunctionalInterface
public interface Predicate<T extends Object> {

    public boolean test(T t);

}

					

java.util.function.Consumer

  • Parameter type: T

  • Return type: void

  • Description: Represents an operation that accepts a single input argument and returns no result.


package java.util.function;

@FunctionalInterface
public interface Consumer<T extends Object> {

    public void accept(T t);

}

					

java.util.function.Function

  • Parameter type: T

  • Return type: R

  • Description: Represents a function that accepts one argument and produces a result.


package java.util.function;

@FunctionalInterface
public interface Function<T extends Object, R extends Object> {

    public R apply(T t);

}

					

java.util.function.Supplier

  • Parameter type: None

  • Return type: T

  • Description: Represents a supplier of results. There is no requirement that a new or distinct result be returned each time the supplier is invoked.


package java.util.function;

@FunctionalInterface
public interface Supplier<T extends Object> {

    public T get();
}

					

java.util.function.ToDoubleFunction

  • Parameter type: T

  • Return type: double

  • Description: Represents a function that produces a double-valued result. This is the double-producing primitive specialization for Function.


package java.util.function;

@FunctionalInterface
public interface ToDoubleFunction<T extends Object> {

    public double applyAsDouble(T t);
}

					

java.util.function.DoubleFunction

  • Parameter type: double

  • Return type: R

  • Description: Represents a function that accepts a double-valued argument and produces a result. This is the double-consuming primitive specialization for Function.


package java.util.function;

@FunctionalInterface
public interface DoubleFunction<R extends Object> {

    public R apply(double d);
}

					

java.util.function.BiPredicate

  • Parameter types: T, U

  • Return type: boolean

  • Description: Represents a predicate (boolean-valued function) of two arguments. This is the two-arity specialization of Predicate.


package java.util.function;

@FunctionalInterface
public interface BiPredicate<T extends Object, U extends Object> {

    public boolean test(T t, U u);

}

					

java.util.function.UnaryOperator

  • Parameter types: T

  • Return type: T

  • Description: Represents an operation on a single operand that produces a result of the same type as its operand. This is a specialization of Function for the case where the operand and result are of the same type.

    NOTE: Since it extends Function, it inherits Single Abstract Method - Function.apply(Object);, or more accurate T apply(T t);


package java.util.function;

@FunctionalInterface
public interface UnaryOperator<T extends Object> extends Function<T, T> {

}

					

[Warning]

You must know listed above interfaces and their main characteristics for the exam.

Professional hosting         Free 'Oracle Certified Expert Web Services Developer 6' Guide     Exam 1Z0-810: Upgrade to Java SE 8 Programmer Quiz